Skip to content

Commit 29b8a7f

Browse files
author
Praful Makani
authored
docs(samples): add query destination table with encryption key (#576)
1 parent 2ac556e commit 29b8a7f

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_query_destination_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.EncryptionConfiguration;
24+
import com.google.cloud.bigquery.QueryJobConfiguration;
25+
import com.google.cloud.bigquery.TableResult;
26+
27+
// Sample to query on destination table with encryption key
28+
public class QueryDestinationTableCMEK {
29+
30+
public static void runQueryDestinationTableCMEK() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String datasetName = "MY_DATASET_NAME";
33+
String tableName = "MY_TABLE_NAME";
34+
String kmsKeyName = "MY_KMS_KEY_NAME";
35+
String query =
36+
String.format("SELECT stringField, booleanField FROM %s.%s", datasetName, tableName);
37+
EncryptionConfiguration encryption =
38+
EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build();
39+
queryDestinationTableCMEK(query, encryption);
40+
}
41+
42+
public static void queryDestinationTableCMEK(String query, EncryptionConfiguration encryption) {
43+
try {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
47+
48+
QueryJobConfiguration config =
49+
QueryJobConfiguration.newBuilder(query)
50+
// Set the encryption key to use for the destination.
51+
.setDestinationEncryptionConfiguration(encryption)
52+
.build();
53+
54+
TableResult results = bigquery.query(config);
55+
56+
results
57+
.iterateAll()
58+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
59+
System.out.println("Query performed successfully with encryption key.");
60+
} catch (BigQueryException | InterruptedException e) {
61+
System.out.println("Query not performed \n" + e.toString());
62+
}
63+
}
64+
}
65+
// [END bigquery_query_destination_table_cmek]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 QueryDestinationTableCMEKIT {
35+
36+
private String tableName;
37+
private EncryptionConfiguration encryption;
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+
// create a test table with encryption key
65+
tableName = "MY_TABLE_CMEK_TEST" + UUID.randomUUID().toString().substring(0, 8);
66+
Schema schema =
67+
Schema.of(
68+
Field.of("stringField", StandardSQLTypeName.STRING),
69+
Field.of("booleanField", StandardSQLTypeName.BOOL));
70+
encryption = EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build();
71+
CreateTableCMEK.createTableCMEK(BIGQUERY_DATASET_NAME, tableName, schema, encryption);
72+
73+
bout = new ByteArrayOutputStream();
74+
out = new PrintStream(bout);
75+
System.setOut(out);
76+
}
77+
78+
@After
79+
public void tearDown() {
80+
// Clean up
81+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
82+
System.setOut(null);
83+
}
84+
85+
@Test
86+
public void testQueryDestinationTableCMEK() {
87+
String query =
88+
String.format(
89+
"SELECT stringField, booleanField FROM %s.%s", BIGQUERY_DATASET_NAME, tableName);
90+
QueryDestinationTableCMEK.queryDestinationTableCMEK(query, encryption);
91+
assertThat(bout.toString()).contains("Query performed successfully with encryption key.");
92+
}
93+
}

0 commit comments

Comments
 (0)