com.google.api.services.bigquery.Bigquery Java Examples

The following examples show how to use com.google.api.services.bigquery.Bigquery. 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: TestBigQuery.java    From beam with Apache License 2.0 6 votes vote down vote up
@Experimental(Kind.SCHEMAS)
public TableDataInsertAllResponse insertRows(Schema rowSchema, Row... rows) throws IOException {
  List<Rows> bqRows =
      Arrays.stream(rows)
          .map(row -> new Rows().setJson(BigQueryUtils.toTableRow(row)))
          .collect(ImmutableList.toImmutableList());
  Bigquery bq = newBigQueryClient(pipelineOptions);

  return bq.tabledata()
      .insertAll(
          pipelineOptions.getProject(),
          pipelineOptions.getTargetDataset(),
          table.getTableReference().getTableId(),
          new TableDataInsertAllRequest().setRows(bqRows))
      .execute();
}
 
Example #2
Source File: BigQueryInterpreter.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Override
public void cancel(InterpreterContext context) {
  logger.info("Trying to Cancel current query statement.");

  if (service != null && jobId != null && projectId != null) {
    try {
      Bigquery.Jobs.Cancel request = service.jobs().cancel(projectId, jobId);
      JobCancelResponse response = request.execute();
      jobId = null;
      logger.info("Query Execution cancelled");
    } catch (IOException ex) {
      logger.error("Could not cancel the SQL execution");
    }
  } else {
    logger.info("Query Execution was already cancelled");
  }
}
 
Example #3
Source File: AbstractBigQueryIoIntegrationTestBase.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
@After
public void tearDown() throws IOException {
  // Delete the test dataset along with all tables inside it.
  // TODO(user): Move this into library shared by BigQueryOutputCommitter.
  Bigquery.Datasets datasets = bigqueryInstance.datasets();
  logger.atInfo().log(
      "Deleting temporary test dataset '%s' for project '%s'", testDataset, projectIdValue);
  datasets.delete(projectIdValue, testDataset).setDeleteContents(true).execute();

  // Recursively delete the testBucket.
  setConfigForGcsFromBigquerySettings();
  Path toDelete = new Path(String.format("gs://%s", testBucket));
  FileSystem fs = toDelete.getFileSystem(config);
  if ("gs".equals(fs.getScheme())) {
    bucketHelper.cleanup(
        GoogleCloudStorageFileSystemIntegrationHelper.createGcsFs(projectIdValue).getGcs());
  } else {
    logger.atInfo().log("Deleting temporary test bucket '%s'", toDelete);
    fs.delete(toDelete, true);
  }
}
 
Example #4
Source File: UpdateSnapshotViewAction.java    From nomulus with Apache License 2.0 6 votes vote down vote up
private static void updateTable(Bigquery bigquery, Table table) throws IOException {
  TableReference ref = table.getTableReference();
  try {
    bigquery
        .tables()
        .update(ref.getProjectId(), ref.getDatasetId(), ref.getTableId(), table)
        .execute();
  } catch (GoogleJsonResponseException e) {
    if (e.getDetails() != null && e.getDetails().getCode() == 404) {
      bigquery.tables().insert(ref.getProjectId(), ref.getDatasetId(), table).execute();
    } else {
      logger.atWarning().withCause(e).log(
          "UpdateSnapshotViewAction failed, caught exception %s", e.getDetails());
    }
  }
}
 
Example #5
Source File: CheckedBigquery.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/** Ensures the table exists in Bigquery. */
private void ensureTable(Bigquery bigquery, TableReference table, List<TableFieldSchema> schema)
    throws IOException {
  try {
    bigquery.tables().insert(table.getProjectId(), table.getDatasetId(), new Table()
        .setSchema(new TableSchema().setFields(schema))
        .setTableReference(table))
        .execute();
    logger.atInfo().log(
        "Created BigQuery table %s:%s.%s",
        table.getProjectId(), table.getDatasetId(), table.getTableId());
  } catch (IOException e) {
    // Swallow errors about a table that exists, and throw any other ones.
    if (!BigqueryJobFailureException.create(e).getReason().equals("duplicate")) {
      throw e;
    }
  }
}
 
Example #6
Source File: CheckedBigquery.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures the dataset exists by trying to create it. Note that it's not appreciably cheaper
 * to check for dataset existence than it is to try to create it and check for exceptions.
 */
// Note that these are not static so they can be mocked for testing.
private void ensureDataset(Bigquery bigquery, String projectId, String datasetId)
    throws IOException {
  try {
    bigquery.datasets()
        .insert(projectId,
            new Dataset().setDatasetReference(
                new DatasetReference()
                    .setProjectId(projectId)
                    .setDatasetId(datasetId)))
        .execute();
  } catch (IOException e) {
    // Swallow errors about a duplicate dataset, and throw any other ones.
    if (!BigqueryJobFailureException.create(e).getReason().equals("duplicate")) {
      throw e;
    }
  }
}
 
Example #7
Source File: CheckedBigquery.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new connection to Bigquery, first ensuring that the given dataset and table exist in
 * project with the given id, creating them if required.
 */
public Bigquery ensureDataSetAndTableExist(String projectId, String datasetId, String tableId)
    throws IOException {
  ensureDataSetExists(projectId, datasetId);
  checkArgument(bigquerySchemas.containsKey(tableId), "Unknown table ID: %s", tableId);

  if (!knownExistingTables.contains(tableId)) {
    ensureTable(
        bigquery,
        new TableReference()
            .setDatasetId(datasetId)
            .setProjectId(projectId)
            .setTableId(tableId),
          bigquerySchemas.get(tableId));
    knownExistingTables.add(tableId);
  }

  return bigquery;
}
 
Example #8
Source File: GcpUtil.java    From digdag with Apache License 2.0 6 votes vote down vote up
static List<DatasetList.Datasets> listDatasets(Bigquery bq, String projectId, Predicate<DatasetList.Datasets> needle)
        throws IOException, RetryExecutor.RetryGiveupException
{
    List<DatasetList.Datasets> datasets = new ArrayList<>();
    Bigquery.Datasets.List req = retryExecutor.run(() -> bq.datasets().list(projectId));
    DatasetList datasetList;
    do {
        datasetList = req.execute();
        if (datasetList.getDatasets() != null) {
            datasetList.getDatasets().stream()
                    .filter(needle)
                    .forEach(datasets::add);
        }
        req.setPageToken(datasetList.getNextPageToken());
    }
    while (null != datasetList.getNextPageToken());
    return datasets;
}
 
Example #9
Source File: GcpUtil.java    From digdag with Apache License 2.0 6 votes vote down vote up
static List<TableList.Tables> listTables(Bigquery bq, String projectId, String datasetId, Predicate<TableList.Tables> needle)
        throws IOException
{
    List<TableList.Tables> tables = new ArrayList<>();
    Bigquery.Tables.List req = bq.tables().list(projectId, datasetId);
    TableList tableList;
    do {
        tableList = req.execute();
        if (tableList.getTables() != null) {
            tableList.getTables().stream().filter(needle).forEach(tables::add);
        }
        req.setPageToken(tableList.getNextPageToken());
    }
    while (null != tableList.getNextPageToken());
    return tables;
}
 
Example #10
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Returns a BigQuery client builder using the specified {@link BigQueryOptions}. */
private static Bigquery.Builder newBigQueryClient(BigQueryOptions options) {
  RetryHttpRequestInitializer httpRequestInitializer =
      new RetryHttpRequestInitializer(ImmutableList.of(404));
  httpRequestInitializer.setCustomErrors(createBigQueryClientCustomErrors());
  httpRequestInitializer.setWriteTimeout(options.getHTTPWriteTimeout());
  return new Bigquery.Builder(
          Transport.getTransport(),
          Transport.getJsonFactory(),
          chainHttpRequestInitializer(
              options.getGcpCredential(),
              // Do not log 404. It clutters the output and is possibly even required by the
              // caller.
              httpRequestInitializer))
      .setApplicationName(options.getAppName())
      .setGoogleClientRequestInitializer(options.getGoogleApiTrace());
}
 
Example #11
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);

  // Set up the MockHttpRequest for future inspection
  request =
      new MockLowLevelHttpRequest() {
        @Override
        public LowLevelHttpResponse execute() throws IOException {
          return response;
        }
      };

  // A mock transport that lets us mock the API responses.
  MockHttpTransport transport =
      new MockHttpTransport.Builder().setLowLevelHttpRequest(request).build();

  // A sample BigQuery API client that uses default JsonFactory and RetryHttpInitializer.
  bigquery =
      new Bigquery.Builder(
              transport, Transport.getJsonFactory(), new RetryHttpRequestInitializer())
          .build();
}
 
Example #12
Source File: GcpUtil.java    From digdag with Apache License 2.0 5 votes vote down vote up
static Table createTable(Bigquery bq, String projectId, String datasetId, String tableId)
        throws IOException
{
    Table table = new Table()
            .setTableReference(new TableReference()
                    .setProjectId(projectId)
                    .setDatasetId(datasetId)
                    .setTableId(tableId));
    Table created = createTable(bq, projectId, datasetId, table);
    assertThat(tableExists(bq, projectId, datasetId, tableId), is(true));
    return created;
}
 
Example #13
Source File: GcpUtil.java    From digdag with Apache License 2.0 5 votes vote down vote up
static boolean datasetExists(Bigquery bq, String projectId, String datasetId)
        throws IOException
{
    try {
        bq.datasets().get(projectId, datasetId).execute();
        return true;
    }
    catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
            return false;
        }
        throw e;
    }
}
 
Example #14
Source File: GcpUtil.java    From digdag with Apache License 2.0 5 votes vote down vote up
static void cleanupBq(Bigquery bq, String gcpProjectId)
        throws IOException, RetryExecutor.RetryGiveupException
{
    if (bq == null) {
        return;
    }
    List<DatasetList.Datasets> datasets = listDatasets(
            bq, gcpProjectId, ds -> ds.getDatasetReference().getDatasetId().contains(BQ_TAG));
    for (DatasetList.Datasets dataset : datasets) {
        deleteDataset(bq, gcpProjectId, dataset.getDatasetReference().getDatasetId());
    }
}
 
Example #15
Source File: BigQueryIT.java    From digdag with Apache License 2.0 5 votes vote down vote up
private Bigquery bqClient(GoogleCredential credential)
{
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(BigqueryScopes.all());
    }
    return new Bigquery.Builder(transport, jsonFactory, credential)
            .setApplicationName("digdag-test")
            .build();
}
 
Example #16
Source File: BqClient.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
protected Bigquery client(GoogleCredential credential, HttpTransport transport, JsonFactory jsonFactory)
{
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(BigqueryScopes.all());
    }

    return new Bigquery.Builder(transport, jsonFactory, credential)
            .setApplicationName("Digdag")
            .build();
}
 
Example #17
Source File: BigQueryInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public static Job pollJob(final Bigquery.Jobs.Get request, final long interval) 
    throws IOException, InterruptedException {
  Job job = request.execute();
  while (!job.getStatus().getState().equals("DONE")) {
    System.out.println("Job is " 
        + job.getStatus().getState() 
        + " waiting " + interval + " milliseconds...");
    Thread.sleep(interval);
    job = request.execute();
  }
  return job;
}
 
Example #18
Source File: GcpUtil.java    From digdag with Apache License 2.0 5 votes vote down vote up
static boolean tableExists(Bigquery bq, String projectId, String datasetId, String tableId)
        throws IOException
{
    try {
        bq.tables().get(projectId, datasetId, tableId).execute();
        return true;
    }
    catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
            return false;
        }
        throw e;
    }
}
 
Example #19
Source File: GcpUtil.java    From digdag with Apache License 2.0 5 votes vote down vote up
static Dataset createDataset(Bigquery bq, String projectId, String datasetId)
        throws IOException, RetryExecutor.RetryGiveupException
{
    Dataset dataset = new Dataset()
            .setDatasetReference(new DatasetReference()
                    .setDatasetId(datasetId));
    Dataset created = createDataset(bq, projectId, dataset);
    assertThat(datasetExists(bq, projectId, datasetId), is(true));
    return created;
}
 
Example #20
Source File: GoogleBigQueryIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    configuration = new GoogleBigQueryConfiguration();

    bigquery = Mockito.mock(Bigquery.class);
    endpoint = Mockito.mock(GoogleBigQueryEndpoint.class);
    tabledata = Mockito.mock(Bigquery.Tabledata.class);
    mockInsertall = Mockito.mock(Bigquery.Tabledata.InsertAll.class);

    Mockito.when(bigquery.tabledata()).thenReturn(tabledata);
    Mockito.when(tabledata.insertAll(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())).thenReturn(mockInsertall);

    TableDataInsertAllResponse mockResponse = new TableDataInsertAllResponse();
    Mockito.when(mockInsertall.execute()).thenReturn(mockResponse);
}
 
Example #21
Source File: BigQueryFactory.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a BigQuery from the credential constructed from the environment.
 *
 * @throws IOException on IO Error.
 * @throws GeneralSecurityException on General Security Error.
 */
public Bigquery getBigQuery(Configuration config)
    throws GeneralSecurityException, IOException {
  logger.atInfo().log("Creating BigQuery from default credential.");
  Credential credential = createBigQueryCredential(config);
  // Use the credential to create an authorized BigQuery client
  return getBigQueryFromCredential(config, credential, BQC_ID);
}
 
Example #22
Source File: BigQueryFactory.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/** Constructs a BigQuery from a given Credential. */
public Bigquery getBigQueryFromCredential(
    Configuration config, Credential credential, String appName) {
  logger.atInfo().log("Creating BigQuery from given credential.");
  // Use the credential to create an authorized BigQuery client
  if (credential != null) {
    return new Bigquery
        .Builder(HTTP_TRANSPORT, JSON_FACTORY, new RetryHttpInitializer(credential, appName))
        .setApplicationName(appName).build();
  }
  return new Bigquery.Builder(HTTP_TRANSPORT, JSON_FACTORY, /* httpRequestInitializer= */ null)
      .setRootUrl(BQ_ROOT_URL.get(config, config::get))
      .setApplicationName(appName)
      .build();
}
 
Example #23
Source File: AbstractBigQueryInputFormat.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Cleans up relevant temporary resources associated with a job which used the
 * GsonBigQueryInputFormat; this should be called explicitly after the completion of the entire
 * job. Possibly cleans up intermediate export tables if configured to use one due to
 * specifying a BigQuery "query" for the input. Cleans up the GCS directoriy where BigQuery
 * exported its files for reading.
 */
public static void cleanupJob(Configuration configuration, JobID jobId) throws IOException {
  String exportPathRoot = BigQueryConfiguration.getTemporaryPathRoot(configuration, jobId);
  configuration.set(TEMP_GCS_PATH.getKey(), exportPathRoot);
  Bigquery bigquery;
  try {
    bigquery = new BigQueryFactory().getBigQuery(configuration);
  } catch (GeneralSecurityException gse) {
    throw new IOException("Failed to create Bigquery client", gse);
  }
  cleanupJob(new BigQueryHelper(bigquery), configuration);
}
 
Example #24
Source File: BigQueryFactoryTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void getBigQuery_localDev() throws Exception {
  BigQueryFactory factory = new BigQueryFactory();
  Configuration configuration = CredentialConfigurationUtil.getTestConfiguration();

  Bigquery bigquery = factory.getBigQuery(configuration);

  assertThat(bigquery).isNotNull();
  assertThat(bigquery.getRootUrl()).isEqualTo("https://bigquery.googleapis.com/");
}
 
Example #25
Source File: BigQueryFactoryTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void getBigQuery_customEndpoint() throws Exception {
  BigQueryFactory factory = new BigQueryFactory();
  Configuration configuration = CredentialConfigurationUtil.getTestConfiguration();
  configuration.set(BQ_ROOT_URL.getKey(), "https://unit-test-bigquery.googleapis.com/");

  Bigquery bigquery = factory.getBigQuery(configuration);

  assertThat(bigquery).isNotNull();
  assertThat(bigquery.getRootUrl()).isEqualTo("https://unit-test-bigquery.googleapis.com/");
}
 
Example #26
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 #27
Source File: ExampleUtils.java    From deployment-examples with MIT License 5 votes vote down vote up
/** Returns a BigQuery client builder using the specified {@link BigQueryOptions}. */
private static Bigquery.Builder newBigQueryClient(BigQueryOptions options) {
  return new Bigquery.Builder(
          Transport.getTransport(),
          Transport.getJsonFactory(),
          chainHttpRequestInitializer(
              options.getGcpCredential(),
              // Do not log 404. It clutters the output and is possibly even required by the
              // caller.
              new RetryHttpRequestInitializer(ImmutableList.of(404))))
      .setApplicationName(options.getAppName())
      .setGoogleClientRequestInitializer(options.getGoogleApiTrace());
}
 
Example #28
Source File: UploadDatastoreBackupAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private String uploadBackup(String backupId, String backupFolderUrl, Iterable<String> kinds)
    throws IOException {
  Bigquery bigquery = checkedBigquery.ensureDataSetExists(projectId, BACKUP_DATASET);
  String loadMessage =
      String.format("Loading Datastore backup %s from %s...", backupId, backupFolderUrl);
  logger.atInfo().log(loadMessage);

  String sanitizedBackupId = sanitizeForBigquery(backupId);
  StringBuilder builder = new StringBuilder(loadMessage + "\n");
  builder.append("Load jobs:\n");

  for (String kindName : kinds) {
    String jobId = String.format("load-backup-%s-%s", sanitizedBackupId, kindName);
    JobReference jobRef = new JobReference().setProjectId(projectId).setJobId(jobId);
    String sourceUri = getBackupInfoFileForKind(backupFolderUrl, kindName);
    String tableId = String.format("%s_%s", sanitizedBackupId, kindName);

    // Launch the load job.
    Job job = makeLoadJob(jobRef, sourceUri, tableId);
    bigquery.jobs().insert(projectId, job).execute();

    // Enqueue a task to check on the load job's completion, and if it succeeds, to update a
    // well-known view in BigQuery to point at the newly loaded backup table for this kind.
    bigqueryPollEnqueuer.enqueuePollTask(
        jobRef,
        createViewUpdateTask(BACKUP_DATASET, tableId, kindName, LATEST_BACKUP_VIEW_NAME),
        getQueue(UpdateSnapshotViewAction.QUEUE));

    builder.append(String.format(" - %s:%s\n", projectId, jobId));
    logger.atInfo().log("Submitted load job %s:%s", projectId, jobId);
  }
  return builder.toString();
}
 
Example #29
Source File: ExampleUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Returns a BigQuery client builder using the specified {@link BigQueryOptions}. */
private static Bigquery.Builder newBigQueryClient(BigQueryOptions options) {
  return new Bigquery.Builder(
          Transport.getTransport(),
          Transport.getJsonFactory(),
          chainHttpRequestInitializer(
              options.getGcpCredential(),
              // Do not log 404. It clutters the output and is possibly even required by the
              // caller.
              new RetryHttpRequestInitializer(ImmutableList.of(404))))
      .setApplicationName(options.getAppName())
      .setGoogleClientRequestInitializer(options.getGoogleApiTrace());
}
 
Example #30
Source File: TestBigQuery.java    From beam with Apache License 2.0 5 votes vote down vote up
private TableSchema getSchema(Bigquery bq) {
  try {
    return bq.tables()
        .get(
            pipelineOptions.getProject(),
            pipelineOptions.getTargetDataset(),
            table.getTableReference().getTableId())
        .execute()
        .getSchema();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}