Skip to content

Commit b3621df

Browse files
author
Praful Makani
authored
docs(samples): add load orc file and overwrite data into a table from gcs (#584)
1 parent f127475 commit b3621df

File tree

2 files changed

+152
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)