@Override public JobId get() { return JobId.of(); } };
@Override public com.google.api.services.bigquery.model.Job call() { return bigQueryRpc.getJob( completeJobId.getProject(), completeJobId.getJob(), completeJobId.getLocation(), optionsMap); } },
JobId setLocation(String location) { return getLocation() != null ? this : toBuilder().setLocation(location).build(); }
JobId setProjectId(String projectId) { return getProject() != null ? this : toBuilder().setProject(projectId).build(); }
@Test public void testGetQueryResults() { JobId queryJob = JobId.of(JOB); GetQueryResultsResponse responsePb = new GetQueryResultsResponse() .setEtag("etag") .setJobReference(queryJob.toPb()) .setRows(ImmutableList.of(TABLE_ROW)) .setJobComplete(true) .setCacheHit(false) .setPageToken(CURSOR) .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); EasyMock.expect(bigqueryRpcMock.getQueryResults(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); QueryResponse response = bigquery.getQueryResults(queryJob); assertEquals(true, response.getCompleted()); assertEquals(null, response.getSchema()); }
@Override public String call() { return options .getBigQueryRpcV2() .open( new com.google.api.services.bigquery.model.Job() .setConfiguration(writeChannelConfiguration.toPb()) .setJobReference(jobId.toPb())); } },
@Override public boolean cancel(JobId jobId) { final JobId completeJobId = jobId .setProjectId(getOptions().getProjectId()) .setLocation( jobId.getLocation() == null && getOptions().getLocation() != null ? getOptions().getLocation() : jobId.getLocation()); try { return runWithRetries( new Callable<Boolean>() { @Override public Boolean call() { return bigQueryRpc.cancel( completeJobId.getProject(), completeJobId.getJob(), completeJobId.getLocation()); } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } }
/** 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] }
/** Creates a job identity given only its user-defined id. */ public static JobId of(String job) { return newBuilder().setJob(checkNotNull(job)).build(); }
bigquery.create( JobInfo.of( JobId.newBuilder().setLocation(location).build(), QueryJobConfiguration.of(query))); job = job.waitFor(); assertThat(job.getStatus().getError()).isNull(); assertThat(job.getJobId().getLocation()).isEqualTo(location); JobId wrongId = jobId.toBuilder().setLocation(wrongLocation).build(); .query( QueryJobConfiguration.of(query), JobId.newBuilder().setLocation(location).build()) .iterateAll()) .isEmpty(); .query( QueryJobConfiguration.of(query), JobId.newBuilder().setLocation(wrongLocation).build()) .iterateAll(); fail("querying a table with wrong location shouldn't work"); try (TableDataWriteChannel writer = bigquery.writer( JobId.newBuilder().setLocation(location).build(), writeChannelConfiguration)) { writer.write(ByteBuffer.wrap("foo".getBytes())); JobId.newBuilder().setLocation(wrongLocation).build(), writeChannelConfiguration);
@Test public void testOf() { assertEquals(null, JOB.getProject()); assertEquals("job", JOB.getJob()); assertEquals("project", JOB_COMPLETE.getProject()); assertEquals("job", JOB_COMPLETE.getJob()); }
BuilderImpl(Job jobPb) { this.etag = jobPb.getEtag(); this.generatedId = jobPb.getId(); if (jobPb.getJobReference() != null) { this.jobId = JobId.fromPb(jobPb.getJobReference()); } this.selfLink = jobPb.getSelfLink(); if (jobPb.getStatus() != null) { this.status = JobStatus.fromPb(jobPb.getStatus()); } if (jobPb.getStatistics() != null) { this.statistics = JobStatistics.fromPb(jobPb); } this.userEmail = jobPb.getUserEmail(); if (jobPb.getConfiguration() != null) { this.configuration = JobConfiguration.fromPb(jobPb.getConfiguration()); } }
JobInfo setProjectId(String projectId) { Builder builder = toBuilder(); if (jobId != null) { builder.setJobId(jobId.setProjectId(projectId)); } return builder.setConfiguration(configuration.setProjectId(projectId)).build(); }
@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()); } } }
@Test public void testSetProjectId() { JobInfo jobInfo = COPY_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); CopyJobConfiguration copyConfiguration = jobInfo.getConfiguration(); assertEquals("p", copyConfiguration.getDestinationTable().getProject()); for (TableId sourceTable : copyConfiguration.getSourceTables()) { assertEquals("p", sourceTable.getProject()); } jobInfo = EXTRACT_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); ExtractJobConfiguration extractConfiguration = jobInfo.getConfiguration(); assertEquals("p", extractConfiguration.getSourceTable().getProject()); jobInfo = LOAD_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); LoadJobConfiguration loadConfiguration = jobInfo.getConfiguration(); assertEquals("p", loadConfiguration.getDestinationTable().getProject()); jobInfo = QUERY_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); QueryJobConfiguration queryConfiguration = jobInfo.getConfiguration(); assertEquals("p", queryConfiguration.getDefaultDataset().getProject()); assertEquals("p", queryConfiguration.getDestinationTable().getProject()); }
@Test public void testGetQueryResultsWithProject() { JobId queryJob = JobId.of(OTHER_PROJECT, JOB); GetQueryResultsResponse responsePb = new GetQueryResultsResponse() .setEtag("etag") .setJobReference(queryJob.toPb()) .setRows(ImmutableList.of(TABLE_ROW)) .setJobComplete(true) .setCacheHit(false) .setPageToken(CURSOR) .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); EasyMock.expect(bigqueryRpcMock.getQueryResults(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); QueryResponse response = bigquery.getQueryResults(queryJob); assertTrue(response.getCompleted()); assertEquals(null, response.getSchema()); }
@Test public void testCreateNonRetryableError() { expect( bigqueryRpcMock.open( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .andThrow(new RuntimeException()); replay(bigqueryRpcMock); thrown.expect(RuntimeException.class); new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); }
final JobId completeJobId = jobId .setProjectId(getOptions().getProjectId()) .setLocation( jobId.getLocation() == null && getOptions().getLocation() != null ? getOptions().getLocation() : jobId.getLocation()); try { com.google.api.services.bigquery.model.Job answer =
/** Creates a job identity given project's and job's user-defined id. */ public static JobId of(String project, String job) { return newBuilder().setProject(checkNotNull(project)).setJob(checkNotNull(job)).build(); }