Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add undelete table (#638)
* docs(samples): add undelete table * docs(samples): update code
- Loading branch information
Praful Makani
committed
Aug 5, 2020
1 parent
a14bc8b
commit e799bb3
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
79 changes: 79 additions & 0 deletions
79
samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} |