Skip to content

Commit a6d9491

Browse files
author
Praful Makani
authored
docs(samples): add get table labels (#540)
1 parent 563157c commit a6d9491

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_get_table_labels]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Table;
24+
import com.google.cloud.bigquery.TableId;
25+
26+
// Sample to get table labels
27+
public class GetTableLabels {
28+
29+
public static void runGetTableLabels() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
String tableName = "MY_TABLE_NAME";
33+
getTableLabels(datasetName, tableName);
34+
}
35+
36+
public static void getTableLabels(String datasetName, String tableName) {
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+
// This example table starts with existing label { color: 'green' }
43+
Table table = bigquery.getTable(TableId.of(datasetName, tableName));
44+
table
45+
.getLabels()
46+
.forEach((key, value) -> System.out.println("Retrieved labels successfully"));
47+
} catch (BigQueryException e) {
48+
System.out.println("Label was not deleted. \n" + e.toString());
49+
}
50+
}
51+
}
52+
// [END bigquery_get_table_labels]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.example.bigquery;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import com.google.cloud.bigquery.Schema;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
import java.util.UUID;
10+
import org.junit.After;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
public class GetTableLabelsIT {
16+
17+
private String tableName;
18+
private ByteArrayOutputStream bout;
19+
private PrintStream out;
20+
21+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
22+
23+
private static String requireEnvVar(String varName) {
24+
String value = System.getenv(varName);
25+
assertNotNull(
26+
"Environment variable " + varName + " is required to perform these tests.",
27+
System.getenv(varName));
28+
return value;
29+
}
30+
31+
@BeforeClass
32+
public static void checkRequirements() {
33+
requireEnvVar("BIGQUERY_DATASET_NAME");
34+
}
35+
36+
@Before
37+
public void setUp() {
38+
bout = new ByteArrayOutputStream();
39+
out = new PrintStream(bout);
40+
System.setOut(out);
41+
// create a temporary table
42+
tableName = "MY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
43+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
44+
// add label on a table
45+
LabelTable.labelTable(BIGQUERY_DATASET_NAME, tableName);
46+
47+
bout = new ByteArrayOutputStream();
48+
out = new PrintStream(bout);
49+
System.setOut(out);
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
// delete a temporary table
55+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
56+
System.setOut(null);
57+
}
58+
59+
@Test
60+
public void testGetTableLabels() {
61+
GetTableLabels.getTableLabels(BIGQUERY_DATASET_NAME, tableName);
62+
assertThat(bout.toString()).contains("Retrieved labels successfully");
63+
}
64+
}

0 commit comments

Comments
 (0)