/** Example of reloading all fields until job status is DONE. */ // [TARGET reload(JobOption...)] public JobStatus.State reload() throws InterruptedException { // [START ] while (!JobStatus.State.DONE.equals(job.getStatus().getState())) { Thread.sleep(1000L); job = job.reload(); } // [END ] return job.getStatus().getState(); }
@Override public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) { return prevResponse != null && !JobStatus.State.DONE.equals(prevResponse.getStatus().getState()); } },
/** Example of reloading status field until job status is DONE. */ // [TARGET reload(JobOption...)] public JobStatus.State reloadStatus() throws InterruptedException { // [START ] while (!JobStatus.State.DONE.equals(job.getStatus().getState())) { Thread.sleep(1000L); job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS)); } // [END ] return job.getStatus().getState(); }
@Test public void testWaitForWithCheckingPeriod() throws InterruptedException { initializeExpectedJob(3); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.RUNNING); // caused by optimizing extra call to ResultRetryAlgorithm#shouldRetry() expect(status.getState()).andReturn(JobStatus.State.RUNNING); expect(status.getState()).andReturn(JobStatus.State.DONE); // caused by optimizing extra call to ResultRetryAlgorithm#shouldRetry() expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); Job runningJob = expectedJob.toBuilder().setStatus(status).build(); Job completedJob = expectedJob.toBuilder().setStatus(status).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(completedJob); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(completedJob); replay(status, bigquery, mockOptions); initializeJob(); assertSame(completedJob, job.waitFor(TEST_RETRY_OPTIONS)); verify(status, mockOptions); }
/** Example of copying multiple tables to a destination. */ public void copyTables(String datasetId, String destinationTableId) throws InterruptedException { generateTableWithDdl(datasetId, "table1"); generateTableWithDdl(datasetId, "table2"); // [START bigquery_copy_table_multiple_source] TableId destinationTable = TableId.of(datasetId, destinationTableId); CopyJobConfiguration configuration = CopyJobConfiguration.newBuilder( destinationTable, Arrays.asList(TableId.of(datasetId, "table1"), TableId.of(datasetId, "table2"))) .build(); // Copy the tables. Job job = bigquery.create(JobInfo.of(configuration)); job = job.waitFor(); // Check the table StandardTableDefinition table = bigquery.getTable(destinationTable).getDefinition(); System.out.println("State: " + job.getStatus().getState()); System.out.printf("Copied %d rows.\n", table.getNumRows()); // [END bigquery_copy_table_multiple_source] }
@Test public void testWaitFor() throws InterruptedException { initializeExpectedJob(2); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.DONE); // caused by optimizing extra call to ResultRetryAlgorithm#shouldRetry() expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); Job completedJob = expectedJob.toBuilder().setStatus(status).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(completedJob); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(completedJob); replay(status, bigquery, mockOptions); initializeJob(); assertSame(completedJob, job.waitFor(TEST_RETRY_OPTIONS)); verify(status, mockOptions); }
/** Example of loading a parquet file from GCS to a table. */ public void loadTableGcsParquet(String datasetName) throws InterruptedException { // [START bigquery_load_table_gcs_parquet] String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.parquet"; TableId tableId = TableId.of(datasetName, "us_states"); LoadJobConfiguration configuration = LoadJobConfiguration.builder(tableId, sourceUri) .setFormatOptions(FormatOptions.parquet()) .build(); // Load the table Job loadJob = bigquery.create(JobInfo.of(configuration)); loadJob = loadJob.waitFor(); // Check the table StandardTableDefinition destinationTable = bigquery.getTable(tableId).getDefinition(); System.out.println("State: " + loadJob.getStatus().getState()); System.out.printf("Loaded %d rows.\n", destinationTable.getNumRows()); // [END bigquery_load_table_gcs_parquet] }
/** * Checks if this job has completed its execution, either failing or succeeding. If the job does * not exist this method returns {@code true}. * * <p>Example of waiting for a job until it reports that it is done. * * <pre>{@code * while (!job.isDone()) { * Thread.sleep(1000L); * } * }</pre> * * @return {@code true} if this job is in {@link JobStatus.State#DONE} state or if it does not * exist, {@code false} if the state is not {@link JobStatus.State#DONE} * @throws BigQueryException upon failure */ public boolean isDone() { checkNotDryRun("isDone"); Job job = bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS)); return job == null || JobStatus.State.DONE.equals(job.getStatus().getState()); } /**
@Test public void testIsDone_True() throws Exception { initializeExpectedJob(2); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)) .andReturn(expectedJob.toBuilder().setStatus(status).build()); replay(status, bigquery); initializeJob(); assertTrue(job.isDone()); verify(status); }
@Test public void testIsDone_False() throws Exception { initializeExpectedJob(2); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.RUNNING); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)) .andReturn(expectedJob.toBuilder().setStatus(status).build()); replay(status, bigquery); initializeJob(); assertFalse(job.isDone()); verify(status); }
System.out.println("State: " + job.getStatus().getState()); System.out.printf("Recovered %d rows.\n", table.getNumRows());
if (job.getStatus() == null || !JobStatus.State.DONE.equals(job.getStatus().getState())) { job = reload();
@Test public void testConstructor() { assertEquals(STATE, JOB_STATUS.getState()); assertEquals(ERROR, JOB_STATUS.getError()); assertEquals(ALL_ERRORS, JOB_STATUS.getExecutionErrors()); assertEquals(STATE, JOB_STATUS_INCOMPLETE1.getState()); assertEquals(ERROR, JOB_STATUS_INCOMPLETE1.getError()); assertEquals(null, JOB_STATUS_INCOMPLETE1.getExecutionErrors()); assertEquals(STATE, JOB_STATUS_INCOMPLETE2.getState()); assertEquals(null, JOB_STATUS_INCOMPLETE2.getError()); assertEquals(null, JOB_STATUS_INCOMPLETE2.getExecutionErrors()); }
loadJob = loadJob.waitFor(); System.out.println("State: " + loadJob.getStatus().getState()); return ((StandardTableDefinition) bigquery.getTable(tableId).getDefinition()).getNumRows();
/** Example of running a batch query. */ public void runBatchQuery() throws TimeoutException, InterruptedException { // [START bigquery_query_batch] // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query) // Run at batch priority, which won't count toward concurrent rate // limit. .setPriority(QueryJobConfiguration.Priority.BATCH) .build(); // 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] }
@Test public void testQueryJobWithDryRun() throws InterruptedException, TimeoutException { String tableName = "test_query_job_table"; String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); TableId destinationTable = TableId.of(DATASET, tableName); QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setDestinationTable(destinationTable) .setDryRun(true) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); assertNull(remoteJob.getJobId().getJob()); assertEquals(DONE, remoteJob.getStatus().getState()); assertNotNull(remoteJob.getConfiguration()); }
@Override public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) { return prevResponse != null && !JobStatus.State.DONE.equals(prevResponse.getStatus().getState()); } },
/** * Checks if this job has completed its execution, either failing or succeeding. If the job does * not exist this method returns {@code true}. * * <p>Example of waiting for a job until it reports that it is done. * * <pre>{@code * while (!job.isDone()) { * Thread.sleep(1000L); * } * }</pre> * * @return {@code true} if this job is in {@link JobStatus.State#DONE} state or if it does not * exist, {@code false} if the state is not {@link JobStatus.State#DONE} * @throws BigQueryException upon failure */ public boolean isDone() { checkNotDryRun("isDone"); Job job = bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS)); return job == null || JobStatus.State.DONE.equals(job.getStatus().getState()); } /**
if (job.getStatus() == null || !JobStatus.State.DONE.equals(job.getStatus().getState())) { job = reload();