Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f356c71

Browse files
author
Praful Makani
authoredJul 7, 2020
docs(samples): add copy cmek table (#513)
1 parent d6ab2d4 commit f356c71

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.EncryptionConfiguration;
23+
import com.google.cloud.bigquery.Field;
24+
import com.google.cloud.bigquery.Schema;
25+
import com.google.cloud.bigquery.StandardSQLTypeName;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
34+
public class CopyTableCMEKIT {
35+
36+
private String sourceTableName;
37+
private String destinationTableName;
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
41+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
42+
private static final String BIGQUERY_KMS_KEY_NAME = requireEnvVar("BIGQUERY_KMS_KEY_NAME");
43+
44+
private static String requireEnvVar(String varName) {
45+
String value = System.getenv(varName);
46+
assertNotNull(
47+
"Environment variable " + varName + " is required to perform these tests.",
48+
System.getenv(varName));
49+
return value;
50+
}
51+
52+
@BeforeClass
53+
public static void checkRequirements() {
54+
requireEnvVar("BIGQUERY_DATASET_NAME");
55+
requireEnvVar("BIGQUERY_KMS_KEY_NAME");
56+
}
57+
58+
@Before
59+
public void setUp() {
60+
bout = new ByteArrayOutputStream();
61+
out = new PrintStream(bout);
62+
System.setOut(out);
63+
64+
sourceTableName = "MY_SOURCE_TABLE_CMEK_TEST" + UUID.randomUUID().toString().substring(0, 8);
65+
destinationTableName =
66+
"MY_DESTINATION_TABLE_CMEK_TEST" + UUID.randomUUID().toString().substring(0, 8);
67+
Schema schema =
68+
Schema.of(
69+
Field.of("stringField", StandardSQLTypeName.STRING),
70+
Field.of("booleanField", StandardSQLTypeName.BOOL));
71+
EncryptionConfiguration configuration =
72+
EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build();
73+
CreateTableCMEK.createTableCMEK(BIGQUERY_DATASET_NAME, sourceTableName, schema, configuration);
74+
75+
bout = new ByteArrayOutputStream();
76+
out = new PrintStream(bout);
77+
System.setOut(out);
78+
}
79+
80+
@After
81+
public void tearDown() {
82+
// Clean up
83+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, sourceTableName);
84+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, destinationTableName);
85+
System.setOut(null);
86+
}
87+
88+
@Test
89+
public void testCopyTableCMEK() {
90+
EncryptionConfiguration configuration =
91+
EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build();
92+
CopyTableCMEK.copyTableCMEK(
93+
BIGQUERY_DATASET_NAME,
94+
sourceTableName,
95+
BIGQUERY_DATASET_NAME,
96+
destinationTableName,
97+
configuration);
98+
assertThat(bout.toString()).contains("Table cmek copied successfully.");
99+
}
100+
}

0 commit comments

Comments
 (0)
Failed to load comments.