Skip to content

Commit a14bc8b

Browse files
author
Praful Makani
authored
refactor(samples): add column load append (#644)
1 parent 1655f51 commit a14bc8b

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

samples/snippets/src/main/java/com/example/bigquery/AddColumnLoadAppend.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@
3636
import java.util.List;
3737
import java.util.UUID;
3838

39+
// Sample to append column in existing table.
3940
public class AddColumnLoadAppend {
4041

41-
public static void runAddColumnLoadAppend() throws Exception {
42+
public static void runAddColumnLoadAppend() {
4243
// TODO(developer): Replace these variables before running the sample.
4344
String datasetName = "MY_DATASET_NAME";
4445
String tableName = "MY_TABLE_NAME";
@@ -65,7 +66,7 @@ public static void runAddColumnLoadAppend() throws Exception {
6566
}
6667

6768
public static void addColumnLoadAppend(
68-
String datasetName, String tableName, String sourceUri, Schema newSchema) throws Exception {
69+
String datasetName, String tableName, String sourceUri, Schema newSchema) {
6970
try {
7071
// Initialize client that will be used to send requests. This client only needs to be created
7172
// once, and can be reused for multiple requests.
@@ -87,19 +88,16 @@ public static void addColumnLoadAppend(
8788

8889
// Load data from a GCS parquet file into the table
8990
// Blocks until this load table job completes its execution, either failing or succeeding.
90-
Job completedJob = loadJob.waitFor();
91+
Job job = loadJob.waitFor();
9192

9293
// Check for errors
93-
if (completedJob == null) {
94-
throw new Exception("Job not executed since it no longer exists.");
95-
} else if (completedJob.getStatus().getError() != null) {
96-
// You can also look at queryJob.getStatus().getExecutionErrors() for all
97-
// errors, not just the latest one.
98-
throw new Exception(
99-
"BigQuery was unable to load into the table due to an error: \n"
100-
+ loadJob.getStatus().getError());
94+
if (job.isDone() && job.getStatus().getError() == null) {
95+
System.out.println("Column successfully added during load append job");
96+
} else {
97+
System.out.println(
98+
"BigQuery was unable to load into the table due to an error:"
99+
+ job.getStatus().getError());
101100
}
102-
System.out.println("Column successfully added during load append job");
103101
} catch (BigQueryException | InterruptedException e) {
104102
System.out.println("Column not added during load append \n" + e.toString());
105103
}

samples/snippets/src/test/java/com/example/bigquery/AddColumnLoadAppendIT.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.example.bigquery;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static junit.framework.TestCase.assertNotNull;
2120

2221
import com.google.cloud.bigquery.Field;
2322
import com.google.cloud.bigquery.LegacySQLTypeName;
@@ -26,21 +25,28 @@
2625
import java.io.PrintStream;
2726
import java.util.ArrayList;
2827
import java.util.List;
28+
import java.util.UUID;
2929
import org.junit.After;
30+
import org.junit.Assert;
3031
import org.junit.Before;
3132
import org.junit.BeforeClass;
3233
import org.junit.Test;
3334

3435
public class AddColumnLoadAppendIT {
36+
37+
private String tableName;
38+
private Schema schema;
3539
private ByteArrayOutputStream bout;
3640
private PrintStream out;
3741

38-
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
42+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
3943

40-
private static void requireEnvVar(String varName) {
41-
assertNotNull(
44+
private static String requireEnvVar(String varName) {
45+
String value = System.getenv(varName);
46+
Assert.assertNotNull(
4247
"Environment variable " + varName + " is required to perform these tests.",
4348
System.getenv(varName));
49+
return value;
4450
}
4551

4652
@BeforeClass
@@ -53,41 +59,41 @@ public void setUp() {
5359
bout = new ByteArrayOutputStream();
5460
out = new PrintStream(bout);
5561
System.setOut(out);
62+
63+
// create a test table.
64+
tableName = "ADD_COLUMN_LOAD_APPEND_TEST_" + UUID.randomUUID().toString().substring(0, 8);
65+
schema =
66+
Schema.of(
67+
Field.newBuilder("name", LegacySQLTypeName.STRING)
68+
.setMode(Field.Mode.REQUIRED)
69+
.build());
70+
71+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
72+
73+
bout = new ByteArrayOutputStream();
74+
out = new PrintStream(bout);
75+
System.setOut(out);
5676
}
5777

5878
@After
5979
public void tearDown() {
80+
// Clean up
81+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
6082
System.setOut(null);
6183
}
6284

6385
@Test
64-
public void testAddColumnLoadAppend() throws Exception {
86+
public void testAddColumnLoadAppend() {
6587
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
66-
67-
String tableName = "ADD_COLUMN_LOAD_APPEND_TEST";
68-
Schema originalSchema =
69-
Schema.of(
70-
Field.newBuilder("name", LegacySQLTypeName.STRING)
71-
.setMode(Field.Mode.REQUIRED)
72-
.build());
73-
74-
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, originalSchema);
75-
76-
List<Field> fields = originalSchema.getFields();
7788
// Adding below additional column during the load job
7889
Field newField =
7990
Field.newBuilder("post_abbr", LegacySQLTypeName.STRING)
8091
.setMode(Field.Mode.NULLABLE)
8192
.build();
82-
List<Field> newFields = new ArrayList<>(fields);
93+
List<Field> newFields = new ArrayList<>(schema.getFields());
8394
newFields.add(newField);
84-
Schema newSchema = Schema.of(newFields);
85-
86-
AddColumnLoadAppend.addColumnLoadAppend(BIGQUERY_DATASET_NAME, tableName, sourceUri, newSchema);
87-
95+
AddColumnLoadAppend.addColumnLoadAppend(
96+
BIGQUERY_DATASET_NAME, tableName, sourceUri, Schema.of(newFields));
8897
assertThat(bout.toString()).contains("Column successfully added during load append job");
89-
90-
// Clean up
91-
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
9298
}
9399
}

0 commit comments

Comments
 (0)