Skip to content

Commit 2ac556e

Browse files
author
Praful Makani
authored
docs(samples): add load json table with encryption key from gcs (#575)
1 parent 263d3b2 commit 2ac556e

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_load_table_gcs_json_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.FormatOptions;
25+
import com.google.cloud.bigquery.Job;
26+
import com.google.cloud.bigquery.JobInfo;
27+
import com.google.cloud.bigquery.LoadJobConfiguration;
28+
import com.google.cloud.bigquery.TableId;
29+
30+
// Sample to load JSON data with configuration key from Cloud Storage into a new BigQuery table
31+
public class LoadJsonFromGCSCMEK {
32+
33+
public static void runLoadJsonFromGCSCMEK() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String datasetName = "MY_DATASET_NAME";
36+
String tableName = "MY_TABLE_NAME";
37+
String kmsKeyName = "MY_KMS_KEY_NAME";
38+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json";
39+
// i.e. projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{cryptoKey}
40+
EncryptionConfiguration encryption =
41+
EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build();
42+
loadJsonFromGCSCMEK(datasetName, tableName, sourceUri, encryption);
43+
}
44+
45+
public static void loadJsonFromGCSCMEK(
46+
String datasetName, String tableName, String sourceUri, EncryptionConfiguration encryption) {
47+
try {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
51+
52+
TableId tableId = TableId.of(datasetName, tableName);
53+
LoadJobConfiguration loadConfig =
54+
LoadJobConfiguration.newBuilder(tableId, sourceUri)
55+
// Set the encryption key to use for the destination.
56+
.setDestinationEncryptionConfiguration(encryption)
57+
.setFormatOptions(FormatOptions.json())
58+
.setAutodetect(true)
59+
.build();
60+
61+
// Load data from a GCS JSON file into the table
62+
Job job = bigquery.create(JobInfo.of(loadConfig));
63+
// Blocks until this load table job completes its execution, either failing or succeeding.
64+
job = job.waitFor();
65+
if (job.isDone()) {
66+
System.out.println("Table loaded succesfully from GCS with configuration key");
67+
} else {
68+
System.out.println(
69+
"BigQuery was unable to load into the table due to an error:"
70+
+ job.getStatus().getError());
71+
}
72+
} catch (BigQueryException | InterruptedException e) {
73+
System.out.println("Column not added during load append \n" + e.toString());
74+
}
75+
}
76+
}
77+
// [END bigquery_load_table_gcs_json_cmek]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class LoadJsonFromGCSCMEKIT {
32+
33+
private String tableName;
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
38+
private static final String BIGQUERY_KMS_KEY_NAME = requireEnvVar("BIGQUERY_KMS_KEY_NAME");
39+
40+
private static String requireEnvVar(String varName) {
41+
String value = System.getenv(varName);
42+
assertNotNull(
43+
"Environment variable " + varName + " is required to perform these tests.",
44+
System.getenv(varName));
45+
return value;
46+
}
47+
48+
@BeforeClass
49+
public static void checkRequirements() {
50+
requireEnvVar("BIGQUERY_DATASET_NAME");
51+
requireEnvVar("BIGQUERY_KMS_KEY_NAME");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
tableName =
57+
"MY_LOAD_JSON_TABLE_CMEK_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8);
58+
bout = new ByteArrayOutputStream();
59+
out = new PrintStream(bout);
60+
System.setOut(out);
61+
}
62+
63+
@After
64+
public void tearDown() {
65+
// Clean up
66+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
67+
System.setOut(null);
68+
}
69+
70+
@Test
71+
public void testLoadJsonFromGCSCMEK() {
72+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json";
73+
EncryptionConfiguration configuration =
74+
EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build();
75+
LoadJsonFromGCSCMEK.loadJsonFromGCSCMEK(
76+
BIGQUERY_DATASET_NAME, tableName, sourceUri, configuration);
77+
assertThat(bout.toString())
78+
.contains("Table loaded succesfully from GCS with configuration key");
79+
}
80+
}

0 commit comments

Comments
 (0)