|
| 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] |
0 commit comments