@Override public QueryResponse call() { return bigquery.getQueryResults(getJobId(), resultsOptions); } },
@Override public Job call() throws Exception { return bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS)); } },
/** * Sends a job cancel request. * * <p>Example of cancelling a job. * * <pre>{@code * if (job.cancel()) { * return true; // job successfully cancelled * } else { * // job not found * } * }</pre> * * @return {@code true} if cancel request was sent successfully, {@code false} if job was not * found * @throws BigQueryException upon failure */ public boolean cancel() { checkNotDryRun("cancel"); return bigquery.cancel(getJobId()); }
@Override void run(BigQuery bigquery, JobInfo job) throws Exception { System.out.println("Creating job"); Job startedJob = bigquery.create(job); while (!startedJob.isDone()) { System.out.println("Waiting for job " + startedJob.getJobId().getJob() + " to complete"); Thread.sleep(1000L); } startedJob = startedJob.reload(); if (startedJob.getStatus().getError() == null) { System.out.println("Job " + startedJob.getJobId().getJob() + " succeeded"); } else { System.out.println("Job " + startedJob.getJobId().getJob() + " failed"); System.out.println("Error: " + startedJob.getStatus().getError()); } } }
/** * Fetches current job's latest information. Returns {@code null} if the job does not exist. * * <p>Example of reloading all fields until job status is DONE. * * <pre>{@code * while (!JobStatus.State.DONE.equals(job.getStatus().getState())) { * Thread.sleep(1000L); * job = job.reload(); * } * }</pre> * * <p>Example of reloading status field until job status is DONE. * * <pre>{@code * while (!JobStatus.State.DONE.equals(job.getStatus().getState())) { * Thread.sleep(1000L); * job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS)); * } * }</pre> * * @param options job options * @return a {@code Job} object with latest information or {@code null} if not found * @throws BigQueryException upon failure */ public Job reload(JobOption... options) { checkNotDryRun("reload"); return bigquery.getJob(getJobId(), options); }
/** * Checks if this job exists. * * <p>Example of checking that a job exists. * * <pre>{@code * if (!job.exists()) { * // job doesn't exist * } * }</pre> * * @return {@code true} if this job exists, {@code false} otherwise * @throws BigQueryException upon failure */ public boolean exists() { checkNotDryRun("exists"); return bigquery.getJob(getJobId(), JobOption.fields()) != null; }
/** * 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()); } /**
getJobId(), ImmutableList.copyOf(job.getStatus().getExecutionErrors()));
/** 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 testListJobs() { Page<Job> jobs = bigquery.listJobs(); for (Job job : jobs.getValues()) { assertNotNull(job.getJobId()); assertNotNull(job.getStatistics()); assertNotNull(job.getStatus()); assertNotNull(job.getUserEmail()); assertNotNull(job.getGeneratedId()); } }
@Test public void testListJobsWithSelectedFields() { Page<Job> jobs = bigquery.listJobs(JobListOption.fields(JobField.USER_EMAIL)); for (Job job : jobs.getValues()) { assertNotNull(job.getJobId()); assertNotNull(job.getStatus()); assertNotNull(job.getUserEmail()); assertNull(job.getStatistics()); assertNull(job.getGeneratedId()); } }
@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()); }
@Test public void testBuilder() { initializeExpectedJob(2); replay(bigquery); Job builtJob = new Job.Builder(serviceMockReturnsOptions, COPY_CONFIGURATION) .setJobId(JOB_ID) .setStatistics(COPY_JOB_STATISTICS) .setJobId(JOB_ID) .setEtag(ETAG) .setGeneratedId(GENERATED_ID) .setSelfLink(SELF_LINK) .setUserEmail(EMAIL) .setStatus(JOB_STATUS) .build(); assertEquals(ETAG, builtJob.getEtag()); assertEquals(GENERATED_ID, builtJob.getGeneratedId()); assertEquals(SELF_LINK, builtJob.getSelfLink()); assertEquals(EMAIL, builtJob.getUserEmail()); assertEquals(JOB_ID, builtJob.getJobId()); assertEquals(JOB_STATUS, builtJob.getStatus()); assertEquals(COPY_CONFIGURATION, builtJob.getConfiguration()); assertEquals(COPY_JOB_STATISTICS, builtJob.getStatistics()); assertSame(serviceMockReturnsOptions, builtJob.getBigQuery()); }
CopyJobConfiguration.of(destinationTable, sourceTable); Job createdJob = bigquery.create(JobInfo.of(copyJobConfiguration)); Job remoteJob = bigquery.getJob(createdJob.getJobId()); assertEquals(createdJob.getJobId(), remoteJob.getJobId()); CopyJobConfiguration createdConfiguration = createdJob.getConfiguration(); CopyJobConfiguration remoteConfiguration = remoteJob.getConfiguration();
Job createdJob = bigquery.create(JobInfo.of(configuration), JobOption.fields(JobField.ETAG)); CopyJobConfiguration createdConfiguration = createdJob.getConfiguration(); assertNotNull(createdJob.getJobId()); assertNotNull(createdConfiguration.getSourceTables()); assertNotNull(createdConfiguration.getDestinationTable()); assertNull(createdJob.getSelfLink()); assertNull(createdJob.getUserEmail()); Job remoteJob = bigquery.getJob(createdJob.getJobId(), JobOption.fields(JobField.ETAG)); CopyJobConfiguration remoteConfiguration = remoteJob.getConfiguration(); assertEquals(createdJob.getJobId(), remoteJob.getJobId()); assertEquals(createdConfiguration.getSourceTables(), remoteConfiguration.getSourceTables()); assertEquals(
@Test public void testQuery() throws InterruptedException { String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); QueryJobConfiguration config = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).build(); Job job = bigquery.create(JobInfo.of(JobId.of(), config)); TableResult result = job.getQueryResults(); assertEquals(QUERY_RESULT_SCHEMA, result.getSchema()); int rowCount = 0; for (FieldValueList row : result.getValues()) { FieldValue timestampCell = row.get(0); assertEquals(timestampCell, row.get("TimestampField")); FieldValue stringCell = row.get(1); assertEquals(stringCell, row.get("StringField")); FieldValue booleanCell = row.get(2); assertEquals(booleanCell, row.get("BooleanField")); assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(false, booleanCell.getBooleanValue()); rowCount++; } assertEquals(2, rowCount); Job job2 = bigquery.getJob(job.getJobId()); JobStatistics.QueryStatistics statistics = job2.getStatistics(); assertNotNull(statistics.getQueryPlan()); }
Job queryJob = bigquery.getJob(remoteJob.getJobId()); JobStatistics.QueryStatistics statistics = queryJob.getStatistics(); assertNotNull(statistics.getQueryPlan());
@Override public QueryResponse call() { return bigquery.getQueryResults(getJobId(), resultsOptions); } },
@Override public Job call() throws Exception { return bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS)); } },