Skip to content

Commit 1a09f63

Browse files
stephaniewang526kurtisvg
authored andcommitted
Add BigQuery delete dataset sample (GoogleCloudPlatform#1758)
* bigquery_delete_dataset sample * update sample based on comments * update sample based on comments - more meaningful print output * fix lint error
1 parent b544bde commit 1a09f63

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2019 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]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.DatasetId;
24+
25+
public class DeleteDataset {
26+
27+
public static void runDeleteDataset() {
28+
// TODO(developer): Replace these variables before running the sample.\
29+
String projectId = "my-project-id";
30+
String datasetName = "my-dataset-name";
31+
deleteDataset(projectId, datasetName);
32+
}
33+
34+
public static void deleteDataset(String projectId, String datasetName) {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests.
37+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38+
39+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
40+
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
41+
if (success) {
42+
System.out.println("Dataset deleted successfully");
43+
} else {
44+
System.out.println("Dataset was not found");
45+
}
46+
}
47+
}
48+
// [END bigquery_delete_dataset]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019 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+
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.DatasetId;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
public class DeleteDatasetIT {
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
@Before
37+
public void setUp() {
38+
bout = new ByteArrayOutputStream();
39+
out = new PrintStream(bout);
40+
System.setOut(out);
41+
}
42+
43+
@After
44+
public void tearDown() {
45+
System.setOut(null);
46+
}
47+
48+
@Test
49+
public void deleteDataset() {
50+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
51+
52+
//create the dataset to be deleted
53+
String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
54+
DatasetInfo datasetInfo = DatasetInfo.newBuilder(generatedDatasetName).build();
55+
bigquery.create(datasetInfo);
56+
57+
//delete the dataset that was just created
58+
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), generatedDatasetName);
59+
DeleteDataset.deleteDataset(datasetId.getProject(), generatedDatasetName);
60+
61+
assertThat(bout.toString()).contains("Dataset deleted successfully");
62+
}
63+
}

0 commit comments

Comments
 (0)