Skip to content
This repository was archived by the owner on Aug 30, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updates
  • Loading branch information
stephaniewang526 committed Jan 17, 2020
commit 2ae7b17300cfd8685766bf89516837695524a144
34 changes: 16 additions & 18 deletions samples/src/main/java/com/example/bigquery/AddColumnLoadAppend.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FormatOptions;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobInfo.SchemaUpdateOption;
import com.google.cloud.bigquery.JobInfo.WriteDisposition;
Expand All @@ -33,6 +34,7 @@
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableId;
import com.google.common.collect.ImmutableList;
import java.util.UUID;

public class AddColumnLoadAppend {

Expand All @@ -58,45 +60,41 @@ public static void addColumnLoadAppend(String datasetName, String tableName, Str
// 'NULLABLE'.
Schema newSchema =
Schema.of(
Field.newBuilder("word", LegacySQLTypeName.STRING)
Field.newBuilder("name", LegacySQLTypeName.STRING)
.setMode(Field.Mode.REQUIRED)
.build(),
Field.newBuilder("word_count", LegacySQLTypeName.STRING)
.setMode(Field.Mode.REQUIRED)
.build(),
Field.newBuilder("corpus", LegacySQLTypeName.STRING)
.setMode(Field.Mode.REQUIRED)
.build(),
Field.newBuilder("corpus_date", LegacySQLTypeName.STRING)
Field.newBuilder("post_abbr", LegacySQLTypeName.STRING)
.setMode(Field.Mode.REQUIRED)
.build(),
// Adding below additional column during the load job
Field.newBuilder("custom_column", LegacySQLTypeName.STRING)
.setMode(Field.Mode.NULLABLE)
.build());

LoadJobConfiguration configuration =
LoadJobConfiguration loadJobConfig =
LoadJobConfiguration.builder(tableId, sourceUri)
.setFormatOptions(FormatOptions.csv())
.setWriteDisposition(WriteDisposition.WRITE_APPEND)
//TODO:investigate these two configs and why they don't work
.setSchema(newSchema)
.setSchemaUpdateOptions(ImmutableList.of(SchemaUpdateOption.ALLOW_FIELD_ADDITION))
.build();

Job job = bigquery.create(JobInfo.of(configuration));
// Create a job ID so that we can safely retry.
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job loadJob = bigquery.create(JobInfo.newBuilder(loadJobConfig).setJobId(jobId).build());
System.out.println(loadJob.getJobId());

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

// Check for errors
if (completedJob == null) {
System.out.println("Job not executed since it no longer exists.");
return;
throw new RuntimeException("Job no longer exists");
Comment thread
stephaniewang526 marked this conversation as resolved.
Outdated
} else if (completedJob.getStatus().getError() != null) {
System.out.println(
"BigQuery was unable to load into the table due to an error: \n"
+ job.getStatus().getError());
return;
// You can also look at queryJob.getStatus().getExecutionErrors() for all
// errors, not just the latest one.
throw new RuntimeException(loadJob.getStatus().getError().toString());
Comment thread
stephaniewang526 marked this conversation as resolved.
Outdated
}

System.out.println("Column successfully added during load append job");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class AddColumnLoadAppendIT {
private PrintStream out;

private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
private static final String GCS_BUCKET = System.getenv("GCS_BUCKET");

private static void requireEnvVar(String varName) {
assertNotNull(
Expand All @@ -43,7 +42,6 @@ private static void requireEnvVar(String varName) {
@BeforeClass
public static void checkRequirements() {
requireEnvVar("BIGQUERY_DATASET_NAME");
requireEnvVar("GCS_BUCKET");
}

// @Before
Expand All @@ -60,21 +58,16 @@ public static void checkRequirements() {

@Test
public void testAddColumnLoadAppend() {
String sourceUri = "gs://" + GCS_BUCKET + "/extractTest.csv";
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";

String tableName = "ADD_COLUMN_LOAD_APPEND_TEST";
Schema originalSchema =
Schema.of(
Field.newBuilder("word", LegacySQLTypeName.STRING).setMode(Field.Mode.REQUIRED).build(),
Field.newBuilder("word_count", LegacySQLTypeName.STRING)
Field.newBuilder("name", LegacySQLTypeName.STRING).setMode(Field.Mode.REQUIRED).build(),
Field.newBuilder("post_abbr", LegacySQLTypeName.STRING)
.setMode(Field.Mode.REQUIRED)
.build(),
Field.newBuilder("corpus", LegacySQLTypeName.STRING)
.setMode(Field.Mode.REQUIRED)
.build(),
Field.newBuilder("corpus_date", LegacySQLTypeName.STRING)
.setMode(Field.Mode.REQUIRED)
.build());
.build()
);

CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, originalSchema);

Expand Down