Skip to content

Commit a0edebb

Browse files
author
Praful Makani
authored
docs(samples): add delete label on a dataset (#515)
1 parent 71803cc commit a0edebb

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_dataset]
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.Dataset;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
// Sample tp deletes a label on a dataset.
28+
public class DeleteLabelDataset {
29+
30+
public static void runDeleteLabelDataset() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String datasetName = "MY_DATASET_NAME";
33+
deleteLabelDataset(datasetName);
34+
}
35+
36+
public static void deleteLabelDataset(String datasetName) {
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 dataset starts with existing label { color: 'green' }
43+
Dataset dataset = bigquery.getDataset(datasetName);
44+
// Add label to dataset
45+
Map<String, String> labels = new HashMap<>();
46+
labels.put("color", null);
47+
48+
dataset.toBuilder().setLabels(labels).build().update();
49+
System.out.println("Dataset label deleted successfully");
50+
} catch (BigQueryException e) {
51+
System.out.println("Dataset label was not deleted. \n" + e.toString());
52+
}
53+
}
54+
}
55+
// [END bigquery_delete_label_dataset]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 com.google.cloud.bigquery.BigQuery;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
import com.google.common.collect.ImmutableMap;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import java.util.Map;
29+
import java.util.UUID;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
35+
public class DeleteLabelDatasetIT {
36+
37+
private String datasetName;
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
41+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
42+
43+
private static String requireEnvVar(String varName) {
44+
String value = System.getenv(varName);
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
return value;
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
54+
}
55+
56+
@Before
57+
public void setUp() {
58+
// create a temporary dataset
59+
datasetName = "MY_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8);
60+
Map<String, String> labels = ImmutableMap.of("color", "green");
61+
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
62+
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).setLabels(labels).build();
63+
bigQuery.create(datasetInfo);
64+
65+
bout = new ByteArrayOutputStream();
66+
out = new PrintStream(bout);
67+
System.setOut(out);
68+
}
69+
70+
@After
71+
public void tearDown() {
72+
// delete a temporary dataset
73+
DeleteDataset.deleteDataset(PROJECT_ID, datasetName);
74+
System.setOut(null);
75+
}
76+
77+
@Test
78+
public void testDeleteLabelDataset() {
79+
DeleteLabelDataset.deleteLabelDataset(datasetName);
80+
assertThat(bout.toString()).contains("Dataset label deleted successfully");
81+
}
82+
}

0 commit comments

Comments
 (0)