Skip to content

Commit 209d035

Browse files
author
Praful Makani
authored
docs(samples): add delete dataset and contents (#629)
1 parent 8d71720 commit 209d035

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_dataset_and_contents]
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.DatasetId;
24+
25+
// Sample to delete dataset with contents.
26+
public class DeleteDatasetAndContents {
27+
28+
public static void runDeleteDatasetAndContents() {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
String datasetName = "MY_DATASET_NAME";
32+
deleteDatasetAndContents(projectId, datasetName);
33+
}
34+
35+
public static void deleteDatasetAndContents(String projectId, String datasetName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
42+
// Use the force parameter to delete a dataset and its contents
43+
boolean success = bigquery.delete(datasetId, BigQuery.DatasetDeleteOption.deleteContents());
44+
if (success) {
45+
System.out.println("Dataset deleted with contents successfully");
46+
} else {
47+
System.out.println("Dataset was not found");
48+
}
49+
} catch (BigQueryException e) {
50+
System.out.println("Dataset was not deleted with contents. \n" + e.toString());
51+
}
52+
}
53+
}
54+
// [END bigquery_delete_dataset_and_contents]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class DeleteDatasetAndContentsIT {
31+
32+
private String datasetName;
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
37+
38+
private static String requireEnvVar(String varName) {
39+
String value = System.getenv(varName);
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
return value;
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
// create a temporary dataset
57+
datasetName = "MY_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8);
58+
CreateDataset.createDataset(datasetName);
59+
60+
bout = new ByteArrayOutputStream();
61+
out = new PrintStream(bout);
62+
System.setOut(out);
63+
}
64+
65+
@After
66+
public void tearDown() {
67+
System.setOut(null);
68+
}
69+
70+
@Test
71+
public void testDeleteDatasetAndContents() {
72+
DeleteDatasetAndContents.deleteDatasetAndContents(PROJECT_ID, datasetName);
73+
assertThat(bout.toString()).contains("Dataset deleted with contents successfully");
74+
}
75+
}

0 commit comments

Comments
 (0)