Skip to content
Permalink
Browse files
docs(samples): add undelete table (#611)
* docs(samples): add undelete table

* docs(samples): update code
  • Loading branch information
Praful Makani committed Jul 29, 2020
1 parent 3698969 commit 6c763d58f2f13f52a44983b56b74b300a17468cc
@@ -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;

// [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;
import org.threeten.bp.Instant;

// 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();

// Record the current time. We'll use this as the snapshot time
// for recovering the table.
long snapTime = Instant.now().toEpochMilli();

// "Accidentally" delete the table.
bigquery.delete(TableId.of(datasetName, tableName));

// 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]
@@ -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.");
}
}

0 comments on commit 6c763d5

Please sign in to comment.