Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
docs(samples): add delete dataset and contents (#629)
  • Loading branch information
Praful Makani committed Jul 31, 2020
1 parent 8d71720 commit 209d035
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
@@ -0,0 +1,54 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

// [START bigquery_delete_dataset_and_contents]
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.DatasetId;

// Sample to delete dataset with contents.
public class DeleteDatasetAndContents {

public static void runDeleteDatasetAndContents() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
deleteDatasetAndContents(projectId, datasetName);
}

public static void deleteDatasetAndContents(String projectId, String datasetName) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

DatasetId datasetId = DatasetId.of(projectId, datasetName);
// Use the force parameter to delete a dataset and its contents
boolean success = bigquery.delete(datasetId, BigQuery.DatasetDeleteOption.deleteContents());
if (success) {
System.out.println("Dataset deleted with contents successfully");
} else {
System.out.println("Dataset was not found");
}
} catch (BigQueryException e) {
System.out.println("Dataset was not deleted with contents. \n" + e.toString());
}
}
}
// [END bigquery_delete_dataset_and_contents]
@@ -0,0 +1,75 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class DeleteDatasetAndContentsIT {

private String datasetName;
private ByteArrayOutputStream bout;
private PrintStream out;

private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
assertNotNull(
"Environment variable " + varName + " is required to perform these tests.",
System.getenv(varName));
return value;
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
// create a temporary dataset
datasetName = "MY_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8);
CreateDataset.createDataset(datasetName);

bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testDeleteDatasetAndContents() {
DeleteDatasetAndContents.deleteDatasetAndContents(PROJECT_ID, datasetName);
assertThat(bout.toString()).contains("Dataset deleted with contents successfully");
}
}

0 comments on commit 209d035

Please sign in to comment.