Skip to content

Commit 6c763d5

Browse files
author
Praful Makani
authored
docs(samples): add undelete table (#611)
* docs(samples): add undelete table * docs(samples): update code
1 parent 3698969 commit 6c763d5

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_undelete_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.CopyJobConfiguration;
24+
import com.google.cloud.bigquery.Job;
25+
import com.google.cloud.bigquery.JobInfo;
26+
import com.google.cloud.bigquery.TableId;
27+
import org.threeten.bp.Instant;
28+
29+
// Sample to undeleting a table
30+
public class UndeleteTable {
31+
32+
public static void runUndeleteTable() {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String datasetName = "MY_DATASET_NAME";
35+
String tableName = "MY_TABLE_TABLE";
36+
String recoverTableName = "MY_RECOVER_TABLE_TABLE";
37+
undeleteTable(datasetName, tableName, recoverTableName);
38+
}
39+
40+
public static void undeleteTable(String datasetName, String tableName, String recoverTableName) {
41+
try {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
45+
46+
// Record the current time. We'll use this as the snapshot time
47+
// for recovering the table.
48+
long snapTime = Instant.now().toEpochMilli();
49+
50+
// "Accidentally" delete the table.
51+
bigquery.delete(TableId.of(datasetName, tableName));
52+
53+
// Construct the restore-from tableID using a snapshot decorator.
54+
String snapshotTableId = String.format("%s@%d", tableName, snapTime);
55+
56+
// Construct and run a copy job.
57+
CopyJobConfiguration configuration =
58+
CopyJobConfiguration.newBuilder(
59+
// Choose a new table ID for the recovered table data.
60+
TableId.of(datasetName, recoverTableName),
61+
TableId.of(datasetName, snapshotTableId))
62+
.build();
63+
64+
Job job = bigquery.create(JobInfo.of(configuration));
65+
job = job.waitFor();
66+
if (job.isDone() && job.getStatus().getError() == null) {
67+
System.out.println("Undelete table recovered successfully.");
68+
} else {
69+
System.out.println(
70+
"BigQuery was unable to copy the table due to an error: \n"
71+
+ job.getStatus().getError());
72+
return;
73+
}
74+
} catch (BigQueryException | InterruptedException e) {
75+
System.out.println("Table not found. \n" + e.toString());
76+
}
77+
}
78+
}
79+
// [END bigquery_undelete_table]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.Schema;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class UndeleteTableIT {
32+
33+
private String tableName;
34+
private String recoverTableName;
35+
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
38+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
39+
40+
private static void requireEnvVar(String varName) {
41+
assertNotNull(
42+
"Environment variable " + varName + " is required to perform these tests.",
43+
System.getenv(varName));
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("BIGQUERY_DATASET_NAME");
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
57+
tableName = "UNDELETE_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
58+
recoverTableName = "RECOVER_DELETE_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
59+
// Create table in dataset for testing
60+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
61+
62+
bout = new ByteArrayOutputStream();
63+
out = new PrintStream(bout);
64+
System.setOut(out);
65+
}
66+
67+
@After
68+
public void tearDown() {
69+
// Clean up
70+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, recoverTableName);
71+
System.setOut(null);
72+
}
73+
74+
@Test
75+
public void testUndeleteTable() {
76+
UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName);
77+
assertThat(bout.toString()).contains("Undelete table recovered successfully.");
78+
}
79+
}

0 commit comments

Comments
 (0)