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 f127475

Browse files
author
Praful Makani
authoredJul 22, 2020
docs(samples): add load json table with overwrite data from gcs (#582)
1 parent cbb1902 commit f127475

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_truncate]
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.Field;
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.Schema;
29+
import com.google.cloud.bigquery.StandardSQLTypeName;
30+
import com.google.cloud.bigquery.TableId;
31+
32+
// Sample to overwrite the BigQuery table data by loading a JSON file from GCS
33+
public class LoadJsonFromGCSTruncate {
34+
35+
public static void runLoadJsonFromGCSTruncate() {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String datasetName = "MY_DATASET_NAME";
38+
String tableName = "MY_TABLE_NAME";
39+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json";
40+
Schema schema =
41+
Schema.of(
42+
Field.of("name", StandardSQLTypeName.STRING),
43+
Field.of("post_abbr", StandardSQLTypeName.STRING));
44+
loadJsonFromGCSTruncate(datasetName, tableName, sourceUri, schema);
45+
}
46+
47+
public static void loadJsonFromGCSTruncate(
48+
String datasetName, String tableName, String sourceUri, Schema schema) {
49+
try {
50+
// Initialize client that will be used to send requests. This client only needs to be created
51+
// once, and can be reused for multiple requests.
52+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
53+
54+
TableId tableId = TableId.of(datasetName, tableName);
55+
LoadJobConfiguration loadConfig =
56+
LoadJobConfiguration.newBuilder(tableId, sourceUri)
57+
.setFormatOptions(FormatOptions.json())
58+
// Set the write disposition to overwrite existing table data
59+
.setWriteDisposition(JobInfo.WriteDisposition.WRITE_TRUNCATE)
60+
.setSchema(schema)
61+
.build();
62+
63+
// Load data from a GCS JSON file into the table
64+
Job job = bigquery.create(JobInfo.of(loadConfig));
65+
// Blocks until this load table job completes its execution, either failing or succeeding.
66+
job = job.waitFor();
67+
if (job.isDone()) {
68+
System.out.println("Table is successfully overwritten by JSON file loaded from GCS");
69+
} else {
70+
System.out.println(
71+
"BigQuery was unable to load into the table due to an error:"
72+
+ job.getStatus().getError());
73+
}
74+
} catch (BigQueryException | InterruptedException e) {
75+
System.out.println("Column not added during load append \n" + e.toString());
76+
}
77+
}
78+
}
79+
// [END bigquery_load_table_gcs_json_truncate]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class LoadJsonFromGCSTruncateIT {
34+
35+
private String tableName;
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
40+
41+
private static String requireEnvVar(String varName) {
42+
String value = System.getenv(varName);
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
return value;
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("BIGQUERY_DATASET_NAME");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
tableName = "MY_LOAD_JSON_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8);
57+
bout = new ByteArrayOutputStream();
58+
out = new PrintStream(bout);
59+
System.setOut(out);
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
// Clean up
65+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
66+
System.setOut(null);
67+
}
68+
69+
@Test
70+
public void loadLoadJsonFromGCSTruncate() {
71+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json";
72+
Schema schema =
73+
Schema.of(
74+
Field.of("name", StandardSQLTypeName.STRING),
75+
Field.of("post_abbr", StandardSQLTypeName.STRING));
76+
LoadJsonFromGCSTruncate.loadJsonFromGCSTruncate(
77+
BIGQUERY_DATASET_NAME, tableName, sourceUri, schema);
78+
assertThat(bout.toString())
79+
.contains("Table is successfully overwritten by JSON file loaded from GCS");
80+
}
81+
}

0 commit comments

Comments
 (0)
Failed to load comments.