Skip to content

Commit b5fd40f

Browse files
author
Praful Makani
authored
docs(samples): add create cmek table (googleapis#511)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [X] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [X] Appropriate docs were updated (if necessary)
1 parent 2a63c66 commit b5fd40f

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_create_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.Field;
25+
import com.google.cloud.bigquery.Schema;
26+
import com.google.cloud.bigquery.StandardSQLTypeName;
27+
import com.google.cloud.bigquery.StandardTableDefinition;
28+
import com.google.cloud.bigquery.TableDefinition;
29+
import com.google.cloud.bigquery.TableId;
30+
import com.google.cloud.bigquery.TableInfo;
31+
32+
// Sample to create a cmek table
33+
public class CreateTableCMEK {
34+
35+
public static void runCreateTableCMEK() {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String datasetName = "MY_DATASET_NAME";
38+
String tableName = "MY_TABLE_NAME";
39+
String kmsKeyName = "MY_KEY_NAME";
40+
Schema schema =
41+
Schema.of(
42+
Field.of("stringField", StandardSQLTypeName.STRING),
43+
Field.of("booleanField", StandardSQLTypeName.BOOL));
44+
// i.e. projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{cryptoKey}
45+
EncryptionConfiguration encryption =
46+
EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build();
47+
createTableCMEK(datasetName, tableName, schema, encryption);
48+
}
49+
50+
public static void createTableCMEK(
51+
String datasetName, String tableName, Schema schema, EncryptionConfiguration configuration) {
52+
try {
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests.
55+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
56+
57+
TableId tableId = TableId.of(datasetName, tableName);
58+
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
59+
TableInfo tableInfo =
60+
TableInfo.newBuilder(tableId, tableDefinition)
61+
.setEncryptionConfiguration(configuration)
62+
.build();
63+
64+
bigquery.create(tableInfo);
65+
System.out.println("Table cmek created successfully");
66+
} catch (BigQueryException e) {
67+
System.out.println("Table cmek was not created. \n" + e.toString());
68+
}
69+
}
70+
}
71+
// [END bigquery_create_table_cmek]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 CreateTableCMEKIT {
35+
36+
private String tableName;
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
40+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
41+
private static final String BIGQUERY_KMS_KEY_NAME = requireEnvVar("BIGQUERY_KMS_KEY_NAME");
42+
43+
private static String requireEnvVar(String varName) {
44+
String value = System.getenv(varName);
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
return value;
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("BIGQUERY_DATASET_NAME");
54+
requireEnvVar("BIGQUERY_KMS_KEY_NAME");
55+
}
56+
57+
@Before
58+
public void setUp() {
59+
tableName = "MY_TABLE_CMEK_TEST" + UUID.randomUUID().toString().substring(0, 8);
60+
bout = new ByteArrayOutputStream();
61+
out = new PrintStream(bout);
62+
System.setOut(out);
63+
}
64+
65+
@After
66+
public void tearDown() {
67+
// Clean up
68+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
69+
System.setOut(null);
70+
}
71+
72+
@Test
73+
public void testCreateTableCMEK() {
74+
Schema schema =
75+
Schema.of(
76+
Field.of("stringField", StandardSQLTypeName.STRING),
77+
Field.of("booleanField", StandardSQLTypeName.BOOL));
78+
EncryptionConfiguration configuration =
79+
EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build();
80+
CreateTableCMEK.createTableCMEK(BIGQUERY_DATASET_NAME, tableName, schema, configuration);
81+
assertThat(bout.toString()).contains("Table cmek created successfully");
82+
}
83+
}

0 commit comments

Comments
 (0)