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