Skip to content

Commit

Permalink
Batch sample now reflects Go and Python versions (#3359)
Browse files Browse the repository at this point in the history
It doesn't make sense to wait synchronously for a batch query to
complete because they could take a long time to get scheduled. Instead,
demonstrate how to poll for the job state (possibly from a different
machine).
  • Loading branch information
tswast authored and pongad committed Jun 8, 2018
1 parent 3939fd6 commit 3ebebd0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.cloud.bigquery.FieldValueList;
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.LoadJobConfiguration;
import com.google.cloud.bigquery.QueryJobConfiguration;
Expand Down Expand Up @@ -182,18 +183,26 @@ public void runBatchQuery() throws TimeoutException, InterruptedException {
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(query)
// Run at batch priority, which won't count toward concurrent rate
// limit. See:
// https://cloud.google.com/bigquery/docs/running-queries#batch
// limit.
.setPriority(QueryJobConfiguration.Priority.BATCH)
.build();

// Print the results.
for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) {
for (FieldValue val : row) {
System.out.printf("%s,", val.toString());
}
System.out.printf("\n");
}
// Location must match that of the dataset(s) referenced in the query.
JobId jobId = JobId.newBuilder().setRandomJob().setLocation("US").build();
String jobIdString = jobId.getJob();

// API request - starts the query.
bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());

// Check on the progress by getting the job's updated state. Once the state
// is `DONE`, the results are ready.
Job queryJob = bigquery.getJob(
JobId.newBuilder().setJob(jobIdString).setLocation("US").build());
System.out.printf(
"Job %s in location %s currently in state: %s%n",
queryJob.getJobId().getJob(),
queryJob.getJobId().getLocation(),
queryJob.getStatus().getState().toString());
// [END bigquery_query_batch]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testRunUncachedQuery() throws TimeoutException, InterruptedException
public void testRunBatchQuery() throws TimeoutException, InterruptedException {
cloudSnippets.runBatchQuery();
String got = bout.toString();
assertTrue(got.contains("romeoandjuliet"));
assertTrue(got.contains("in location US currently in state:"));
}

@Test
Expand Down

0 comments on commit 3ebebd0

Please sign in to comment.