Skip to content

Commit d7cf47d

Browse files
author
Praful Makani
authored
docs(samples): add label table (googleapis#527)
1 parent d775818 commit d7cf47d

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_label_table]
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+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
// Sample to adds a label to an existing table
29+
public class LabelTable {
30+
31+
public static void runLabelTable() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String datasetName = "MY_DATASET_NAME";
34+
String tableName = "MY_TABLE_NAME";
35+
labelTable(datasetName, tableName);
36+
}
37+
38+
public static void labelTable(String datasetName, String tableName) {
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+
44+
// This example table starts with existing label { color: 'green' }
45+
Table table = bigquery.getTable(TableId.of(datasetName, tableName));
46+
// Add label to table
47+
Map<String, String> labels = new HashMap<>();
48+
labels.put("color", "green");
49+
50+
table.toBuilder().setLabels(labels).build().update();
51+
System.out.println("Label added successfully");
52+
} catch (BigQueryException e) {
53+
System.out.println("Label was not added. \n" + e.toString());
54+
}
55+
}
56+
}
57+
// [END bigquery_label_table]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 org.junit.Assert.assertNotNull;
21+
22+
import com.google.cloud.bigquery.Schema;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class LabelTableIT {
32+
33+
private String tableName;
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
38+
39+
private static String requireEnvVar(String varName) {
40+
String value = System.getenv(varName);
41+
assertNotNull(
42+
"Environment variable " + varName + " is required to perform these tests.",
43+
System.getenv(varName));
44+
return value;
45+
}
46+
47+
@BeforeClass
48+
public static void checkRequirements() {
49+
requireEnvVar("BIGQUERY_DATASET_NAME");
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
58+
// create a temporary table
59+
tableName = "MY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
60+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
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 table
70+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
71+
System.setOut(null);
72+
}
73+
74+
@Test
75+
public void testLabelTable() {
76+
LabelTable.labelTable(BIGQUERY_DATASET_NAME, tableName);
77+
assertThat(bout.toString()).contains("Label added successfully");
78+
}
79+
}

0 commit comments

Comments
 (0)