Skip to content

Commit 511e9d7

Browse files
author
Praful Makani
authored
docs(samples): add relax column load append (#657)
* docs(samples): add relax column load append * docs(samples): address feedback
1 parent 4ce0ec8 commit 511e9d7

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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_relax_column_load_append]
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.CsvOptions;
24+
import com.google.cloud.bigquery.Field;
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.Table;
31+
import com.google.cloud.bigquery.TableId;
32+
import com.google.common.collect.ImmutableList;
33+
34+
// Sample to append relax column in a table.
35+
public class RelaxColumnLoadAppend {
36+
37+
public static void main(String[] args) {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String datasetName = "MY_DATASET_NAME";
40+
String tableName = "MY_TABLE_NAME";
41+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
42+
relaxColumnLoadAppend(datasetName, tableName, sourceUri);
43+
}
44+
45+
public static void relaxColumnLoadAppend(String datasetName, String tableName, String sourceUri) {
46+
try {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests.
49+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
50+
51+
// Retrieve destination table reference
52+
Table table = bigquery.getTable(TableId.of(datasetName, tableName));
53+
54+
// column as a 'REQUIRED' field.
55+
Field name =
56+
Field.newBuilder("name", StandardSQLTypeName.STRING).setMode(Field.Mode.REQUIRED).build();
57+
Field postAbbr =
58+
Field.newBuilder("post_abbr", StandardSQLTypeName.STRING)
59+
.setMode(Field.Mode.REQUIRED)
60+
.build();
61+
Schema schema = Schema.of(name, postAbbr);
62+
63+
// Skip header row in the file.
64+
CsvOptions csvOptions = CsvOptions.newBuilder().setSkipLeadingRows(1).build();
65+
66+
// Set job options
67+
LoadJobConfiguration loadConfig =
68+
LoadJobConfiguration.newBuilder(table.getTableId(), sourceUri)
69+
.setSchema(schema)
70+
.setFormatOptions(csvOptions)
71+
.setSchemaUpdateOptions(
72+
ImmutableList.of(JobInfo.SchemaUpdateOption.ALLOW_FIELD_RELAXATION))
73+
.setWriteDisposition(JobInfo.WriteDisposition.WRITE_APPEND)
74+
.build();
75+
76+
// Create a load job and wait for it to complete.
77+
Job job = bigquery.create(JobInfo.of(loadConfig));
78+
job = job.waitFor();
79+
// Check the job's status for errors
80+
if (job.isDone() && job.getStatus().getError() == null) {
81+
System.out.println("Relax column append successfully loaded in a table");
82+
} else {
83+
System.out.println(
84+
"BigQuery was unable to load into the table due to an error:"
85+
+ job.getStatus().getError());
86+
}
87+
} catch (BigQueryException | InterruptedException e) {
88+
System.out.println("Column not added during load append \n" + e.toString());
89+
}
90+
}
91+
}
92+
// [END bigquery_relax_column_load_append]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 RelaxColumnLoadAppendIT {
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+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
System.setOut(out);
59+
60+
// Create a test table
61+
tableName = "RELAX_COLUMN_LOAD_APPEND_TEST_" + UUID.randomUUID().toString().substring(0, 8);
62+
Field id =
63+
Field.newBuilder("id", StandardSQLTypeName.INT64).setMode(Field.Mode.REQUIRED).build();
64+
Field name =
65+
Field.newBuilder("name", StandardSQLTypeName.STRING).setMode(Field.Mode.REQUIRED).build();
66+
Field postAbbr =
67+
Field.newBuilder("post_abbr", StandardSQLTypeName.STRING)
68+
.setMode(Field.Mode.REQUIRED)
69+
.build();
70+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of(id, name, postAbbr));
71+
72+
bout = new ByteArrayOutputStream();
73+
out = new PrintStream(bout);
74+
System.setOut(out);
75+
}
76+
77+
@After
78+
public void tearDown() {
79+
// Clean up
80+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
81+
System.setOut(null);
82+
}
83+
84+
@Test
85+
public void testRelaxColumnLoadAppend() {
86+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
87+
RelaxColumnLoadAppend.relaxColumnLoadAppend(BIGQUERY_DATASET_NAME, tableName, sourceUri);
88+
assertThat(bout.toString()).contains("Relax column append successfully loaded in a table");
89+
}
90+
}

0 commit comments

Comments
 (0)