9951 explained code solutions for 126 technologies


google-big-queryHow do I use the Google BigQuery API with Java?


The Google BigQuery API can be used with Java to access and manipulate data stored in Google BigQuery. To get started, you will need to set up the necessary credentials and libraries.

  1. Set up credentials:
  1. Install the Java BigQuery client library:
  • Add the following line to your pom.xml file:
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-bigquery</artifactId>
    <version>1.94.0</version>
</dependency>
  • Install the library with Maven:
mvn install
  1. Use the Java BigQuery client library:
  • Create a BigQuery object with the credentials file:
BigQuery bigquery = BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
  • Run a query:
String query = "SELECT word FROM `bigquery-public-data.samples.shakespeare` GROUP BY word;";
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
// Create a job ID so that we can safely retry.
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
// Wait for the query to complete.
queryJob = queryJob.waitFor();
// Check for errors
if (queryJob == null) {
    throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
    // You can also look at queryJob.getStatus().getExecutionErrors() for all
    // errors, not just the latest one.
    throw new RuntimeException(queryJob.getStatus().getError().toString());
}
// Get the results.
TableResult result = queryJob.getQueryResults();
  • Print the results:
for (FieldValueList row : result.iterateAll()) {
    String word = row.get("word").getStringValue();
    System.out.println("word: " + word);
}

Output example

word: A
word: All
word: Am
word: ...

For more information, see the BigQuery Java API reference.

Edit this code on GitHub