From e799bb39022b6914678b085a000132d2fbca4a7f Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Wed, 5 Aug 2020 18:18:05 +0530 Subject: [PATCH] docs(samples): add undelete table (#638) * docs(samples): add undelete table * docs(samples): update code --- .../com/example/bigquery/UndeleteTable.java | 78 ++++++++++++++++++ .../com/example/bigquery/UndeleteTableIT.java | 79 +++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java create mode 100644 samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java diff --git a/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java new file mode 100644 index 000000000..5f64c3ffe --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java @@ -0,0 +1,78 @@ +/* + * 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_undelete_table] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.CopyJobConfiguration; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.TableId; + +// Sample to undeleting a table +public class UndeleteTable { + + public static void runUndeleteTable() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_TABLE"; + String recoverTableName = "MY_RECOVER_TABLE_TABLE"; + undeleteTable(datasetName, tableName, recoverTableName); + } + + public static void undeleteTable(String datasetName, String tableName, String recoverTableName) { + 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(); + + // "Accidentally" delete the table. + bigquery.delete(TableId.of(datasetName, tableName)); + + // Record the current time. We'll use this as the snapshot time + // for recovering the table. + long snapTime = System.currentTimeMillis(); + + // Construct the restore-from tableID using a snapshot decorator. + String snapshotTableId = String.format("%s@%d", tableName, snapTime); + + // Construct and run a copy job. + CopyJobConfiguration configuration = + CopyJobConfiguration.newBuilder( + // Choose a new table ID for the recovered table data. + TableId.of(datasetName, recoverTableName), + TableId.of(datasetName, snapshotTableId)) + .build(); + + Job job = bigquery.create(JobInfo.of(configuration)); + job = job.waitFor(); + if (job.isDone() && job.getStatus().getError() == null) { + System.out.println("Undelete table recovered successfully."); + } else { + System.out.println( + "BigQuery was unable to copy the table due to an error: \n" + + job.getStatus().getError()); + return; + } + } catch (BigQueryException | InterruptedException e) { + System.out.println("Table not found. \n" + e.toString()); + } + } +} +// [END bigquery_undelete_table] diff --git a/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java b/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java new file mode 100644 index 000000000..d26290bd8 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java @@ -0,0 +1,79 @@ +/* + * 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 com.google.cloud.bigquery.Schema; +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 UndeleteTableIT { + + private String tableName; + private String recoverTableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + tableName = "UNDELETE_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8); + recoverTableName = "RECOVER_DELETE_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8); + // Create table in dataset for testing + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of()); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, recoverTableName); + System.setOut(null); + } + + @Test + public void testUndeleteTable() { + UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName); + assertThat(bout.toString()).contains("Undelete table recovered successfully."); + } +}