Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 743bc0a

Browse files
authoredJan 14, 2020
feat: sample - Get dataset properties (#98)
* feat: sample - Get dataset properties * nit: update * update base on comments
1 parent f41a53c commit 743bc0a

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
 
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 com.google.api.gax.paging.Page;
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQuery.TableListOption;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Dataset;
25+
import com.google.cloud.bigquery.DatasetId;
26+
import com.google.cloud.bigquery.Table;
27+
28+
// [START bigquery_get_dataset]
29+
public class GetDatasetInfo {
30+
31+
public static void runGetDatasetInfo() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "MY_PROJECT_ID";
34+
String datasetName = "MY_DATASET_NAME";
35+
getDatasetInfo(projectId, datasetName);
36+
}
37+
38+
public static void getDatasetInfo(String projectId, String datasetName) {
39+
try {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
44+
Dataset dataset = bigquery.getDataset(datasetId);
45+
46+
// View dataset properties
47+
String description = dataset.getDescription();
48+
System.out.println(description);
49+
50+
// View tables in the dataset
51+
// For more information on listing tables see:
52+
// https://javadoc.io/static/com.google.cloud/google-cloud-bigquery/0.22.0-beta/com/google/cloud/bigquery/BigQuery.html
53+
Page<Table> tables = bigquery.listTables(datasetName, TableListOption.pageSize(100));
54+
55+
tables.iterateAll().forEach(table -> System.out.print(table.getTableId().getTable() + "\n"));
56+
57+
System.out.println("Dataset info retrieved successfully.");
58+
} catch (BigQueryException e) {
59+
System.out.println("Dataset info not retrieved. \n" + e.toString());
60+
}
61+
}
62+
}
63+
// [END bigquery_get_dataset]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
public class GetDatasetInfoIT {
30+
private ByteArrayOutputStream bout;
31+
private PrintStream out;
32+
33+
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("BIGQUERY_PROJECT_ID");
34+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
35+
36+
private static void requireEnvVar(String varName) {
37+
assertNotNull(
38+
"Environment variable " + varName + " is required to perform these tests.",
39+
System.getenv(varName));
40+
}
41+
42+
@BeforeClass
43+
public static void checkRequirements() {
44+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
45+
requireEnvVar("BIGQUERY_DATASET_NAME");
46+
}
47+
48+
@Before
49+
public void setUp() {
50+
bout = new ByteArrayOutputStream();
51+
out = new PrintStream(bout);
52+
System.setOut(out);
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
System.setOut(null);
58+
}
59+
60+
@Test
61+
public void getDatasetInfo() {
62+
GetDatasetInfo.getDatasetInfo(GOOGLE_CLOUD_PROJECT, BIGQUERY_DATASET_NAME);
63+
assertThat(bout.toString()).contains("Dataset info retrieved successfully.");
64+
}
65+
}

0 commit comments

Comments
 (0)
Failed to load comments.