Skip to content

Commit a0d7bea

Browse files
author
Praful Makani
authored
docs(samples): add list dataset by label (#555)
1 parent 48cdc92 commit a0d7bea

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
// [START bigquery_list_datasets_by_label]
20+
import com.google.api.gax.paging.Page;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Dataset;
25+
26+
// Sample to get list of datasets by label
27+
public class ListDatasetsByLabel {
28+
29+
public static void runListDatasetsByLabel() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "MY_PROJECT_ID";
32+
String filter = "MY_LABEL_FILTER";
33+
listDatasetsByLabel(projectId, filter);
34+
}
35+
36+
public static void listDatasetsByLabel(String projectId, String filter) {
37+
try {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
41+
42+
Page<Dataset> datasets =
43+
bigquery.listDatasets(
44+
projectId,
45+
BigQuery.DatasetListOption.pageSize(100),
46+
BigQuery.DatasetListOption.labelFilter(filter)); // "labels.color:green"
47+
if (datasets == null) {
48+
System.out.println("Dataset does not contain any models");
49+
return;
50+
}
51+
datasets
52+
.iterateAll()
53+
.forEach(
54+
dataset -> System.out.printf("Success! Dataset ID: %s ", dataset.getDatasetId()));
55+
} catch (BigQueryException e) {
56+
System.out.println("Project does not contain any datasets \n" + e.toString());
57+
}
58+
}
59+
}
60+
// [END bigquery_list_datasets_by_label]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class ListDatasetsByLabelIT {
31+
32+
private String datasetName;
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
37+
38+
private static String requireEnvVar(String varName) {
39+
String value = System.getenv(varName);
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
return value;
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
// create a temporary dataset
57+
datasetName = "MY_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8);
58+
CreateDataset.createDataset(datasetName);
59+
// add a label on dataset
60+
LabelDataset.labelDataset(datasetName);
61+
62+
bout = new ByteArrayOutputStream();
63+
out = new PrintStream(bout);
64+
System.setOut(out);
65+
}
66+
67+
@After
68+
public void tearDown() {
69+
// delete a temporary dataset
70+
DeleteDataset.deleteDataset(PROJECT_ID, datasetName);
71+
System.setOut(null);
72+
}
73+
74+
@Test
75+
public void testListDatasetsByLabel() {
76+
String filter = "labels.color:green";
77+
ListDatasetsByLabel.listDatasetsByLabel(PROJECT_ID, filter);
78+
assertThat(bout.toString()).contains("Success! Dataset ID");
79+
}
80+
}

0 commit comments

Comments
 (0)