Skip to content

Commit 8a1caa6

Browse files
feat: new sample - Tables: Delete table (#90)
1 parent e0c923a commit 8a1caa6

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_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.TableId;
24+
25+
public class DeleteTable {
26+
27+
public static void runDeleteTable() {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String datasetName = "MY_DATASET_NAME";
30+
String tableName = "MY_TABLE_NAME";
31+
deleteTable(datasetName, tableName);
32+
}
33+
34+
public static void deleteTable(String datasetName, String tableName) {
35+
try {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests.
38+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
39+
boolean success = bigquery.delete(TableId.of(datasetName, tableName));
40+
if (success) {
41+
System.out.println("Table deleted successfully");
42+
} else {
43+
System.out.println("Table was not found");
44+
}
45+
} catch (BigQueryException e) {
46+
System.out.println("Table was not deleted. \n" + e.toString());
47+
}
48+
}
49+
}
50+
// [END bigquery_delete_table]

samples/src/test/java/com/example/bigquery/CopyMultipleTablesIT.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,8 @@ public void testCopyMultipleTables() {
6969

7070
CopyMultipleTables.copyMultipleTables(BIGQUERY_DATASET_NAME, generatedTableName);
7171
assertThat(bout.toString()).contains("Table copied successfully.");
72+
73+
//Clean up
74+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, generatedTableName);
7275
}
7376
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 DeleteTableIT {
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
35+
36+
private static void requireEnvVar(String varName) {
37+
assertNotNull(
38+
"Environment variable " + varName + " is required to perform these tests.",
39+
System.getenv(varName));
40+
}
41+
42+
@BeforeClass
43+
public static void checkRequirements() {
44+
requireEnvVar("BIGQUERY_DATASET_NAME");
45+
}
46+
47+
@Before
48+
public void setUp() {
49+
bout = new ByteArrayOutputStream();
50+
out = new PrintStream(bout);
51+
System.setOut(out);
52+
}
53+
54+
@After
55+
public void tearDown() {
56+
System.setOut(null);
57+
}
58+
59+
@Test
60+
public void testDeleteTable() {
61+
// Create a new table to be deleted
62+
String generatedTableName =
63+
"gcloud_test_table_temp_" + UUID.randomUUID().toString().replace('-', '_');
64+
CreateTable.createTable(BIGQUERY_DATASET_NAME, generatedTableName, null);
65+
66+
// Delete the table that was just created
67+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, generatedTableName);
68+
69+
assertThat(bout.toString()).contains("Table deleted successfully");
70+
}
71+
}

0 commit comments

Comments
 (0)