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 9c1b82a

Browse files
author
Praful Makani
authoredJul 8, 2020
docs(samples): add delete label on a table (#516)
1 parent a0edebb commit 9c1b82a

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
 
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_delete_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 tp deletes a label on a table.
29+
public class DeleteLabelTable {
30+
31+
public static void runDeleteLabelTable() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String datasetName = "MY_DATASET_NAME";
34+
String tableName = "MY_TABLE_NAME";
35+
deleteLabelTable(datasetName, tableName);
36+
}
37+
38+
public static void deleteLabelTable(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", null);
49+
50+
table.toBuilder().setLabels(labels).build().update();
51+
System.out.println("Table label deleted successfully");
52+
} catch (BigQueryException e) {
53+
System.out.println("Table label was not deleted. \n" + e.toString());
54+
}
55+
}
56+
}
57+
// [START bigquery_delete_label_table]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.BigQuery;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Schema;
25+
import com.google.cloud.bigquery.StandardTableDefinition;
26+
import com.google.cloud.bigquery.TableId;
27+
import com.google.cloud.bigquery.TableInfo;
28+
import com.google.common.collect.ImmutableMap;
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.PrintStream;
31+
import java.util.Map;
32+
import java.util.UUID;
33+
import org.junit.After;
34+
import org.junit.Before;
35+
import org.junit.BeforeClass;
36+
import org.junit.Test;
37+
38+
public class DeleteLabelTableIT {
39+
40+
private String tableName;
41+
private ByteArrayOutputStream bout;
42+
private PrintStream out;
43+
44+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
45+
46+
private static String requireEnvVar(String varName) {
47+
String value = System.getenv(varName);
48+
assertNotNull(
49+
"Environment variable " + varName + " is required to perform these tests.",
50+
System.getenv(varName));
51+
return value;
52+
}
53+
54+
@BeforeClass
55+
public static void checkRequirements() {
56+
requireEnvVar("BIGQUERY_DATASET_NAME");
57+
}
58+
59+
@Before
60+
public void setUp() {
61+
// create a temporary table
62+
tableName = "MY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
63+
Map<String, String> labels = ImmutableMap.of("color", "green");
64+
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
65+
TableId tableId = TableId.of(BIGQUERY_DATASET_NAME, tableName);
66+
StandardTableDefinition tableDefinition = StandardTableDefinition.of(Schema.of());
67+
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).setLabels(labels).build();
68+
bigQuery.create(tableInfo);
69+
70+
bout = new ByteArrayOutputStream();
71+
out = new PrintStream(bout);
72+
System.setOut(out);
73+
}
74+
75+
@After
76+
public void tearDown() {
77+
// delete a temporary table
78+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
79+
System.setOut(null);
80+
}
81+
82+
@Test
83+
public void testDeleteLabelTable() {
84+
DeleteLabelTable.deleteLabelTable(BIGQUERY_DATASET_NAME, tableName);
85+
assertThat(bout.toString()).contains("Table label deleted successfully");
86+
}
87+
}

0 commit comments

Comments
 (0)
Failed to load comments.