Skip to content

Commit 3a62206

Browse files
feat: new sample - Models: List model resources contained in a dataset (#125)
* feat: new sample - Models: List Models * update to use hamcrest truth * update base on comments * update base on comment * nit * nit * nit update * nit update
1 parent a03670b commit 3a62206

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

samples/src/main/java/com/example/bigquery/ListDatasets.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ public static void listDatasets(String projectId) {
3939
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
4040

4141
Page<Dataset> datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100));
42-
for (Dataset dataset : datasets.iterateAll()) {
43-
System.out.println(dataset.getDatasetId() + " dataset in project listed successfully");
42+
if (datasets == null) {
43+
System.out.println("Dataset does not contain any models");
44+
return;
4445
}
46+
datasets
47+
.iterateAll()
48+
.forEach(
49+
dataset -> System.out.printf("Success! Dataset ID: %s ", dataset.getDatasetId()));
4550
} catch (BigQueryException e) {
4651
System.out.println("Project does not contain any datasets \n" + e.toString());
4752
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.api.gax.paging.Page;
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQuery.ModelListOption;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Model;
25+
26+
// [START bigquery_list_models]
27+
public class ListModels {
28+
29+
public static void runListModels() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
listModels(datasetName);
33+
}
34+
35+
public static void listModels(String datasetName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
Page<Model> models = bigquery.listModels(datasetName, ModelListOption.pageSize(100));
42+
if (models == null) {
43+
System.out.println("Dataset does not contain any models.");
44+
return;
45+
}
46+
models
47+
.iterateAll()
48+
.forEach(model -> System.out.printf("Success! Model ID: %s", model.getModelId()));
49+
} catch (BigQueryException e) {
50+
System.out.println("Models not listed in dataset due to error: \n" + e.toString());
51+
}
52+
}
53+
}
54+
// [END bigquery_list_models]

samples/src/test/java/com/example/bigquery/ListDatasetsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public void tearDown() {
4444
public void listDatasets() {
4545
// List datasets in bigquery-public-data project
4646
ListDatasets.listDatasets("bigquery-public-data");
47-
assertThat(bout.toString()).contains("dataset in project listed successfully");
47+
assertThat(bout.toString()).contains("Success! Dataset ID");
4848
}
4949
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static junit.framework.TestCase.assertNotNull;
20+
21+
import com.google.common.truth.Truth;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
public class ListModelsIT {
30+
private ByteArrayOutputStream bout;
31+
private PrintStream out;
32+
33+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
34+
35+
private static void requireEnvVar(String varName) {
36+
assertNotNull(
37+
"Environment variable " + varName + " is required to perform these tests.",
38+
System.getenv(varName));
39+
}
40+
41+
@BeforeClass
42+
public static void checkRequirements() {
43+
requireEnvVar("BIGQUERY_DATASET_NAME");
44+
}
45+
46+
@Before
47+
public void setUp() {
48+
bout = new ByteArrayOutputStream();
49+
out = new PrintStream(bout);
50+
System.setOut(out);
51+
}
52+
53+
@After
54+
public void tearDown() {
55+
System.setOut(null);
56+
}
57+
58+
@Test
59+
public void testListModels() {
60+
ListModels.listModels(BIGQUERY_DATASET_NAME);
61+
Truth.assertThat(bout.toString()).contains("Success! Model ID");
62+
}
63+
}

0 commit comments

Comments
 (0)