|
| 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_copy_table_cmek] |
| 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.EncryptionConfiguration; |
| 25 | +import com.google.cloud.bigquery.Job; |
| 26 | +import com.google.cloud.bigquery.JobInfo; |
| 27 | +import com.google.cloud.bigquery.TableId; |
| 28 | + |
| 29 | +// Sample to copy a cmek table |
| 30 | +public class CopyTableCMEK { |
| 31 | + |
| 32 | + public static void runCopyTableCMEK() { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + String destinationDatasetName = "MY_DESTINATION_DATASET_NAME"; |
| 35 | + String destinationTableId = "MY_DESTINATION_TABLE_NAME"; |
| 36 | + String sourceDatasetName = "MY_SOURCE_DATASET_NAME"; |
| 37 | + String sourceTableId = "MY_SOURCE_TABLE_NAME"; |
| 38 | + String kmsKeyName = "MY_KMS_KEY_NAME"; |
| 39 | + EncryptionConfiguration encryption = |
| 40 | + EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build(); |
| 41 | + copyTableCMEK( |
| 42 | + sourceDatasetName, sourceTableId, destinationDatasetName, destinationTableId, encryption); |
| 43 | + } |
| 44 | + |
| 45 | + public static void copyTableCMEK( |
| 46 | + String sourceDatasetName, |
| 47 | + String sourceTableId, |
| 48 | + String destinationDatasetName, |
| 49 | + String destinationTableId, |
| 50 | + EncryptionConfiguration encryption) { |
| 51 | + try { |
| 52 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 53 | + // once, and can be reused for multiple requests. |
| 54 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 55 | + |
| 56 | + TableId sourceTable = TableId.of(sourceDatasetName, sourceTableId); |
| 57 | + TableId destinationTable = TableId.of(destinationDatasetName, destinationTableId); |
| 58 | + |
| 59 | + // For more information on CopyJobConfiguration see: |
| 60 | + // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/bigquery/JobConfiguration.html |
| 61 | + CopyJobConfiguration configuration = |
| 62 | + CopyJobConfiguration.newBuilder(destinationTable, sourceTable) |
| 63 | + .setDestinationEncryptionConfiguration(encryption) |
| 64 | + .build(); |
| 65 | + |
| 66 | + // For more information on Job see: |
| 67 | + // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html |
| 68 | + Job job = bigquery.create(JobInfo.of(configuration)); |
| 69 | + |
| 70 | + // Blocks until this job completes its execution, either failing or succeeding. |
| 71 | + Job completedJob = job.waitFor(); |
| 72 | + if (completedJob == null) { |
| 73 | + System.out.println("Job not executed since it no longer exists."); |
| 74 | + return; |
| 75 | + } else if (completedJob.getStatus().getError() != null) { |
| 76 | + System.out.println( |
| 77 | + "BigQuery was unable to copy table due to an error: \n" + job.getStatus().getError()); |
| 78 | + return; |
| 79 | + } |
| 80 | + System.out.println("Table cmek copied successfully."); |
| 81 | + } catch (BigQueryException | InterruptedException e) { |
| 82 | + System.out.println("Table cmek copying job was interrupted. \n" + e.toString()); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +// [END bigquery_copy_table_cmek] |
0 commit comments