com.google.api.services.bigquery.model.Job Java Examples

The following examples show how to use com.google.api.services.bigquery.model.Job. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that {@link BigQueryServicesImpl.JobServiceImpl#startLoadJob} succeeds with an already
 * exist job.
 */
@Test
public void testStartLoadJobSucceedsAlreadyExists() throws IOException, InterruptedException {
  Job testJob = new Job();
  JobReference jobRef = new JobReference();
  jobRef.setJobId("jobId");
  jobRef.setProjectId("projectId");
  testJob.setJobReference(jobRef);

  when(response.getStatusCode()).thenReturn(409); // 409 means already exists

  Sleeper sleeper = new FastNanoClockAndSleeper();
  JobServiceImpl.startJob(
      testJob,
      new ApiErrorExtractor(),
      bigquery,
      sleeper,
      BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff()));

  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
  expectedLogs.verifyNotLogged("Started BigQuery job");
}
 
Example #2
Source File: FakeJobService.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public void startExtractJob(JobReference jobRef, JobConfigurationExtract extractConfig)
    throws IOException {
  checkArgument(
      "AVRO".equals(extractConfig.getDestinationFormat()), "Only extract to AVRO is supported");
  synchronized (allJobs) {
    verifyUniqueJobId(jobRef.getJobId());
    ++numExtractJobCalls;

    Job job = new Job();
    job.setJobReference(jobRef);
    job.setConfiguration(new JobConfiguration().setExtract(extractConfig));
    job.setKind(" bigquery#job");
    job.setStatus(new JobStatus().setState("PENDING"));
    allJobs.put(jobRef.getProjectId(), jobRef.getJobId(), new JobInfo(job));
  }
}
 
Example #3
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Checks completed job for errors.
 *
 * @throws BigqueryJobFailureException
 */
private static Job checkJob(Job job) {
  verify(job.getStatus() != null);
  JobStatus jobStatus = job.getStatus();
  if (jobStatus.getErrorResult() != null) {
    throw BigqueryJobFailureException.create(jobStatus);
  } else {
    logger.atInfo().log(summarizeCompletedJob(job));
    if (jobStatus.getErrors() != null) {
      for (ErrorProto error : jobStatus.getErrors()) {
        logger.atWarning().log("%s: %s", error.getReason(), error.getMessage());
      }
    }
    return job;
  }
}
 
Example #4
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/** Create a table from a SQL query if it doesn't already exist. */
public TableReference ensureTable(TableReference table, String sqlQuery) {
  try {
    runJob(new Job()
        .setConfiguration(new JobConfiguration()
            .setQuery(new JobConfigurationQuery()
                .setQuery(sqlQuery)
                .setDefaultDataset(getDataset())
                .setDestinationTable(table))));
  } catch (BigqueryJobFailureException e) {
    if (e.getReason().equals("duplicate")) {
      // Table already exists.
    } else {
      throw e;
    }
  }
  return table;
}
 
Example #5
Source File: BigqueryPollJobAction.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the provided job succeeded, false if it failed, and throws an exception if it
 * is still pending.
 */
private boolean checkJobOutcome() {
  Job job = null;
  String jobRefString =
      toJobReferenceString(new JobReference().setProjectId(projectId).setJobId(jobId));

  try {
    job = bigquery.jobs().get(projectId, jobId).execute();
  } catch (IOException e) {
    // We will throw a new exception because done==false, but first log this exception.
    logger.atWarning().withCause(e).log("Error checking outcome of BigQuery job %s.", jobId);
  }
  // If job is not yet done, then throw an exception so that we'll return a failing HTTP status
  // code and the task will be retried.
  if (job == null || !job.getStatus().getState().equals("DONE")) {
    throw new NotModifiedException(jobRefString);
  }

  // Check if the job ended with an error.
  if (job.getStatus().getErrorResult() != null) {
    logger.atSevere().log("Bigquery job failed - %s - %s", jobRefString, job);
    return false;
  }
  logger.atInfo().log("Bigquery job succeeded - %s", jobRefString);
  return true;
}
 
Example #6
Source File: FakeJobService.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public Job pollJob(JobReference jobRef, int maxAttempts) throws InterruptedException {
  BackOff backoff =
      BackOffAdapter.toGcpBackOff(
          FluentBackoff.DEFAULT
              .withMaxRetries(maxAttempts)
              .withInitialBackoff(Duration.millis(10))
              .withMaxBackoff(Duration.standardSeconds(1))
              .backoff());
  Sleeper sleeper = Sleeper.DEFAULT;
  try {
    do {
      Job job = getJob(jobRef);
      if (job != null) {
        JobStatus status = job.getStatus();
        if (status != null
            && ("DONE".equals(status.getState()) || "FAILED".equals(status.getState()))) {
          return job;
        }
      }
    } while (BackOffUtils.next(sleeper, backoff));
  } catch (IOException e) {
    return null;
  }
  return null;
}
 
Example #7
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Starts an asynchronous job to extract the specified source table and output it to the
 * given GCS filepath in the specified destination format, optionally printing headers.
 * Returns a ListenableFuture that holds the destination GCS URI on success.
 */
private ListenableFuture<String> extractTable(
    DestinationTable sourceTable,
    String destinationUri,
    DestinationFormat destinationFormat,
    boolean printHeader) {
  checkArgument(sourceTable.type == TableType.TABLE);
  Job job = new Job()
      .setConfiguration(new JobConfiguration()
          .setExtract(new JobConfigurationExtract()
              .setSourceTable(sourceTable.getTableReference())
              .setDestinationFormat(destinationFormat.toString())
              .setDestinationUris(ImmutableList.of(destinationUri))
              .setPrintHeader(printHeader)));
  return runJobToCompletion(job, destinationUri);
}
 
Example #8
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public JobStatistics dryRunQuery(
    String projectId, JobConfigurationQuery queryConfig, String location)
    throws InterruptedException, IOException {
  JobReference jobRef = new JobReference().setLocation(location).setProjectId(projectId);
  Job job =
      new Job()
          .setJobReference(jobRef)
          .setConfiguration(new JobConfiguration().setQuery(queryConfig).setDryRun(true));
  return executeWithRetries(
          client.jobs().insert(projectId, job),
          String.format(
              "Unable to dry run query: %s, aborting after %d retries.",
              queryConfig, MAX_RPC_RETRIES),
          Sleeper.DEFAULT,
          createDefaultBackoff(),
          ALWAYS_RETRY)
      .getStatistics();
}
 
Example #9
Source File: FakeJobService.java    From beam with Apache License 2.0 6 votes vote down vote up
private JobStatus runExtractJob(Job job, JobConfigurationExtract extract)
    throws InterruptedException, IOException {
  TableReference sourceTable = extract.getSourceTable();

  List<TableRow> rows =
      datasetService.getAllRows(
          sourceTable.getProjectId(), sourceTable.getDatasetId(), sourceTable.getTableId());
  TableSchema schema = datasetService.getTable(sourceTable).getSchema();
  List<Long> destinationFileCounts = Lists.newArrayList();
  for (String destination : extract.getDestinationUris()) {
    destinationFileCounts.add(writeRows(sourceTable.getTableId(), rows, schema, destination));
  }
  job.setStatistics(
      new JobStatistics()
          .setExtract(new JobStatistics4().setDestinationUriFileCounts(destinationFileCounts)));
  return new JobStatus().setState("DONE");
}
 
Example #10
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#startLoadJob} succeeds. */
@Test
public void testStartLoadJobSucceeds() throws IOException, InterruptedException {
  Job testJob = new Job();
  JobReference jobRef = new JobReference();
  jobRef.setJobId("jobId");
  jobRef.setProjectId("projectId");
  testJob.setJobReference(jobRef);

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  Sleeper sleeper = new FastNanoClockAndSleeper();
  JobServiceImpl.startJob(
      testJob,
      new ApiErrorExtractor(),
      bigquery,
      sleeper,
      BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff()));

  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
  expectedLogs.verifyInfo(String.format("Started BigQuery job: %s", jobRef));
}
 
Example #11
Source File: BigQueryHelperTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertJobOrFetchDuplicateAlreadyExistsException() throws IOException {
  IOException fakeConflictException = new IOException("fake 409 conflict");
  when(mockBigqueryJobsInsert.execute()).thenThrow(fakeConflictException);
  when(mockErrorExtractor.itemAlreadyExists(any(IOException.class))).thenReturn(true);
  when(mockBigqueryJobsGet.execute()).thenReturn(jobHandle);

  assertThat(helper.insertJobOrFetchDuplicate(jobProjectId, jobHandle)).isEqualTo(jobHandle);

  verify(mockBigquery, times(2)).jobs();
  ArgumentCaptor<Job> jobCaptor = ArgumentCaptor.forClass(Job.class);
  verify(mockBigqueryJobs, times(1)).insert(eq(jobProjectId), jobCaptor.capture());
  Job job = jobCaptor.getValue();
  assertThat(job).isEqualTo(jobHandle);
  verify(mockBigqueryJobsInsert, times(1)).execute();
  verify(mockBigqueryJobs, times(1)).get(eq(jobProjectId), eq(jobId));
  verify(mockBigqueryJobsGet, times(1)).execute();
  verify(mockErrorExtractor).itemAlreadyExists(eq(fakeConflictException));
}
 
Example #12
Source File: UploadDatastoreBackupActionTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Before
public void before() throws Exception {
  when(checkedBigquery.ensureDataSetExists("Project-Id", BACKUP_DATASET)).thenReturn(bigquery);
  when(bigquery.jobs()).thenReturn(bigqueryJobs);
  when(bigqueryJobs.insert(eq("Project-Id"), any(Job.class))).thenReturn(bigqueryJobsInsert);
  when(bigquery.datasets()).thenReturn(bigqueryDatasets);
  when(bigqueryDatasets.insert(eq("Project-Id"), any(Dataset.class)))
      .thenReturn(bigqueryDatasetsInsert);
  action = new UploadDatastoreBackupAction();
  action.checkedBigquery = checkedBigquery;
  action.bigqueryPollEnqueuer = bigqueryPollEnqueuer;
  action.projectId = "Project-Id";
  action.backupFolderUrl = "gs://bucket/path";
  action.backupId = "2018-12-05T17:46:39_92612";
  action.backupKinds = "one,two,three";
}
 
Example #13
Source File: BigQueryHelperTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertJobOrFetchDuplicateUnhandledException() throws IOException {
  IOException unhandledException = new IOException("unhandled exception");
  when(mockBigqueryJobsInsert.execute()).thenThrow(unhandledException);
  when(mockErrorExtractor.itemAlreadyExists(any(IOException.class))).thenReturn(false);

  IOException thrown =
      assertThrows(
          IOException.class, () -> helper.insertJobOrFetchDuplicate(jobProjectId, jobHandle));
  assertThat(thrown).hasMessageThat().contains(jobHandle.toString());
  assertThat(thrown).hasCauseThat().isEqualTo(unhandledException);

  verify(mockBigquery, times(1)).jobs();
  ArgumentCaptor<Job> jobCaptor = ArgumentCaptor.forClass(Job.class);
  verify(mockBigqueryJobs, times(1)).insert(eq(jobProjectId), jobCaptor.capture());
  Job job = jobCaptor.getValue();
  assertThat(job).isEqualTo(jobHandle);
  verify(mockBigqueryJobsInsert, times(1)).execute();
  verify(mockErrorExtractor).itemAlreadyExists(eq(unhandledException));
}
 
Example #14
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#pollJob} succeeds. */
@Test
public void testPollJobSucceeds() throws IOException, InterruptedException {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus().setState("DONE"));

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.pollJob(jobRef, Sleeper.DEFAULT, BackOff.ZERO_BACKOFF);

  assertEquals(testJob, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #15
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#pollJob} fails. */
@Test
public void testPollJobFailed() throws IOException, InterruptedException {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus().setState("DONE").setErrorResult(new ErrorProto()));

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.pollJob(jobRef, Sleeper.DEFAULT, BackOff.ZERO_BACKOFF);

  assertEquals(testJob, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #16
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#pollJob} returns UNKNOWN. */
@Test
public void testPollJobUnknown() throws IOException, InterruptedException {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus());

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.pollJob(jobRef, Sleeper.DEFAULT, BackOff.STOP_BACKOFF);

  assertEquals(null, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #17
Source File: BigQueryIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
void cleanup(PassThroughThenCleanup.ContextContainer c) throws Exception {
  PipelineOptions options = c.getPipelineOptions();
  BigQueryOptions bqOptions = options.as(BigQueryOptions.class);
  String jobUuid = c.getJobId();
  final String extractDestinationDir =
      resolveTempLocation(bqOptions.getTempLocation(), "BigQueryExtractTemp", jobUuid);
  final String executingProject = bqOptions.getProject();
  JobReference jobRef =
      new JobReference()
          .setProjectId(executingProject)
          .setJobId(getExtractJobId(createJobIdToken(bqOptions.getJobName(), jobUuid)));

  Job extractJob = getBigQueryServices().getJobService(bqOptions).getJob(jobRef);

  if (extractJob != null) {
    List<ResourceId> extractFiles =
        getExtractFilePaths(extractDestinationDir, extractJob);
    if (extractFiles != null && !extractFiles.isEmpty()) {
      FileSystems.delete(
          extractFiles, MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES);
    }
  }
}
 
Example #18
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetJobSucceeds() throws Exception {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus());

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.getJob(jobRef, Sleeper.DEFAULT, BackOff.ZERO_BACKOFF);

  assertEquals(testJob, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #19
Source File: BigQueryUtilsTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Mocks result of BigQuery for polling for job completion.
 *
 * @throws IOException on IOError.
 */
@Before
public void setUp() throws IOException {

  jobReference = new JobReference().setJobId("test-job-id").setLocation("test-job-location");

  // Create the unfinished job result.
  notDoneJob = new Job();
  notDoneJobStatus = new JobStatus();
  notDoneJobStatus.setState("NOT DONE");
  notDoneJobStatus.setErrorResult(null);
  notDoneJob.setStatus(notDoneJobStatus);
  notDoneJob.setJobReference(jobReference);

  // Create the finished job result.
  job = new Job();
  jobStatus = new JobStatus();
  jobStatus.setState("DONE");
  jobStatus.setErrorResult(null);
  job.setStatus(jobStatus);
  job.setJobReference(jobReference);

  // Mock BigQuery.
  mockBigQuery = mock(Bigquery.class);
  mockBigQueryJobs = mock(Bigquery.Jobs.class);
  mockJobsGet = mock(Bigquery.Jobs.Get.class);
  when(mockBigQuery.jobs()).thenReturn(mockBigQueryJobs);
  when(mockBigQueryJobs.get(projectId, jobReference.getJobId()))
      .thenReturn(mockJobsGet)
      .thenReturn(mockJobsGet);
  when(mockJobsGet.setLocation(any(String.class))).thenReturn(mockJobsGet);
  when(mockJobsGet.execute()).thenReturn(job);

  // Constructor coverage
  new BigQueryUtils();

  // Mock Progressable.
  mockProgressable = mock(Progressable.class);
}
 
Example #20
Source File: BqClient.java    From digdag with Apache License 2.0 5 votes vote down vote up
void submitJob(String projectId, Job job)
        throws IOException
{
    client.jobs()
            .insert(projectId, job)
            .execute();
}
 
Example #21
Source File: BqClient.java    From digdag with Apache License 2.0 5 votes vote down vote up
Job jobStatus(String projectId, String jobId)
        throws IOException
{
    return client.jobs()
            .get(projectId, jobId)
            .execute();
}
 
Example #22
Source File: BaseBqJobOperator.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
protected TaskResult run(BqClient bq, String projectId)
{
    BqJobRunner jobRunner = new BqJobRunner(request, bq, projectId);
    Job completed = jobRunner.runJob(jobConfiguration(projectId));
    return result(completed);
}
 
Example #23
Source File: AbstractExportToCloudStorage.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void beginExport() throws IOException {
  // Create job and configuration.
  JobConfigurationExtract extractConfig = new JobConfigurationExtract();

  // Set source.
  extractConfig.setSourceTable(tableToExport.getTableReference());

  // Set destination.
  extractConfig.setDestinationUris(getExportPaths());
  extractConfig.set(DESTINATION_FORMAT_KEY, fileFormat.getFormatIdentifier());

  JobConfiguration config = new JobConfiguration();
  config.setExtract(extractConfig);

  JobReference jobReference =
      bigQueryHelper.createJobReference(
          projectId, "exporttocloudstorage", tableToExport.getLocation());

  Job job = new Job();
  job.setConfiguration(config);
  job.setJobReference(jobReference);

  // Insert and run job.
  try {
    Job response = bigQueryHelper.insertJobOrFetchDuplicate(projectId, job);
    logger.atFine().log("Got response '%s'", response);
    exportJobReference = response.getJobReference();
  } catch (IOException e) {
    String error = String.format(
        "Error while exporting table %s",
        BigQueryStrings.toString(tableToExport.getTableReference()));
    throw new IOException(error, e);
  }
}
 
Example #24
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Starts an asynchronous query job to dump the results of the specified query into a local
 * ImmutableTable object, row-keyed by the row number (indexed from 1), column-keyed by the
 * TableFieldSchema for that column, and with the value object as the cell value.  Note that null
 * values will not actually be null, but they can be checked for using Data.isNull().
 *
 * <p>Returns a ListenableFuture that holds the ImmutableTable on success.
 */
public ListenableFuture<ImmutableTable<Integer, TableFieldSchema, Object>>
    queryToLocalTable(String querySql) {
  Job job = new Job()
      .setConfiguration(new JobConfiguration()
          .setQuery(new JobConfigurationQuery()
              .setQuery(querySql)
              .setDefaultDataset(getDataset())));
  return transform(runJobToCompletion(job), this::getQueryResults, directExecutor());
}
 
Example #25
Source File: BigQueryHelper.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Helper to check for non-null Job.getJobReference().getJobId() and quality of the getJobId()
 * between {@code expected} and {@code actual}, using Preconditions.checkState.
 */
public void checkJobIdEquality(Job expected, Job actual) {
  Preconditions.checkState(
      actual.getJobReference() != null
          && actual.getJobReference().getJobId() != null
          && expected.getJobReference() != null
          && expected.getJobReference().getJobId() != null
          && actual.getJobReference().getJobId().equals(expected.getJobReference().getJobId()),
      "jobIds must match in '[expected|actual].getJobReference()' (got '%s' vs '%s')",
      expected.getJobReference(),
      actual.getJobReference());
}
 
Example #26
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Synchronously waits for a job to complete that's already been launched.
 *
 * @throws BigqueryJobFailureException
 */
private Job waitForJob(Job job) {
  verify(job.getStatus() != null);
  while (!job.getStatus().getState().equals("DONE")) {
    sleeper.sleepUninterruptibly(pollInterval);
    JobReference ref = job.getJobReference();
    try {
      job = bigquery.jobs().get(ref.getProjectId(), ref.getJobId()).execute();
    } catch (IOException e) {
      throw BigqueryJobFailureException.create(e);
    }
  }
  return job;
}
 
Example #27
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Launch a job, but do not wait for it to complete.
 *
 * @throws BigqueryJobFailureException
 */
private Job launchJob(Job job, @Nullable AbstractInputStreamContent data) {
  verify(job.getStatus() == null);
  try {
    return data != null
        ? bigquery.jobs().insert(getProjectId(), job, data).execute()
        : bigquery.jobs().insert(getProjectId(), job).execute();
  } catch (IOException e) {
    throw BigqueryJobFailureException.create(e);
  }
}
 
Example #28
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the query results for the given job as an ImmutableTable, row-keyed by row number
 * (indexed from 1), column-keyed by the TableFieldSchema for that field, and with the value
 * object as the cell value.  Note that null values will not actually be null (since we're using
 * ImmutableTable) but they can be checked for using Data.isNull().
 *
 * <p>This table is fully materialized in memory (not lazily loaded), so it should not be used
 * with queries expected to return large results.
 */
private ImmutableTable<Integer, TableFieldSchema, Object> getQueryResults(Job job) {
  try {
    ImmutableTable.Builder<Integer, TableFieldSchema, Object> builder =
        new ImmutableTable.Builder<>();
    String pageToken = null;
    int rowNumber = 1;
    while (true) {
      GetQueryResultsResponse queryResults = bigquery.jobs()
            .getQueryResults(getProjectId(), job.getJobReference().getJobId())
            .setPageToken(pageToken)
            .execute();
      // If the job isn't complete yet, retry; getQueryResults() waits for up to 10 seconds on
      // each invocation so this will effectively poll for completion.
      if (queryResults.getJobComplete()) {
        List<TableFieldSchema> schemaFields = queryResults.getSchema().getFields();
        for (TableRow row : queryResults.getRows()) {
          Iterator<TableFieldSchema> fieldIterator = schemaFields.iterator();
          Iterator<TableCell> cellIterator = row.getF().iterator();
          while (fieldIterator.hasNext() && cellIterator.hasNext()) {
            builder.put(rowNumber, fieldIterator.next(), cellIterator.next().getV());
          }
          rowNumber++;
        }
        pageToken = queryResults.getPageToken();
        if (pageToken == null) {
          break;
        }
      }
    }
    return builder.build();
  } catch (IOException e) {
    throw BigqueryJobFailureException.create(e);
  }
}
 
Example #29
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the result of calling queryToLocalTable, but synchronously to avoid spawning new
 * background threads, which App Engine doesn't support.
 *
 * @see <a href="https://cloud.google.com/appengine/docs/standard/java/runtime#Threads">App Engine
 *     Runtime</a>
 */
public ImmutableTable<Integer, TableFieldSchema, Object> queryToLocalTableSync(String querySql) {
  Job job = new Job()
      .setConfiguration(new JobConfiguration()
          .setQuery(new JobConfigurationQuery()
              .setQuery(querySql)
              .setDefaultDataset(getDataset())));
  return getQueryResults(runJob(job));
}
 
Example #30
Source File: BigQueryHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
boolean pollJob() throws IOException {
  if (started) {
    Job job = pollJob.apply(currentJobId);
    this.lastJobAttempted = job;
    Status jobStatus = parseStatus(job);
    switch (jobStatus) {
      case SUCCEEDED:
        LOG.info("Load job {} succeeded. Statistics: {}", currentJobId, job.getStatistics());
        return true;
      case UNKNOWN:
        // This might happen if BigQuery's job listing is slow. Retry with the same
        // job id.
        LOG.info(
            "Load job {} finished in unknown state: {}: {}",
            currentJobId,
            job.getStatus(),
            shouldRetry() ? "will retry" : "will not retry");
        return false;
      case FAILED:
        String oldJobId = currentJobId.getJobId();
        currentJobId = BigQueryHelpers.getRetryJobId(currentJobId, lookupJob).jobId;
        LOG.info(
            "Load job {} failed, {}: {}. Next job id {}",
            oldJobId,
            shouldRetry() ? "will retry" : "will not retry",
            job.getStatus(),
            currentJobId);
        return false;
      default:
        throw new IllegalStateException(
            String.format(
                "Unexpected status [%s] of load job: %s.",
                job.getStatus(), BigQueryHelpers.jobToPrettyString(job)));
    }
  }
  return false;
}