diff --git a/bigquery/README.md b/bigquery/README.md index 6df6ef5455b..341ba38176b 100644 --- a/bigquery/README.md +++ b/bigquery/README.md @@ -1,19 +1,25 @@ -# Getting Started with BigQuery and the Google Java API Client library +# Getting Started with BigQuery -Google's BigQuery Service features a REST-based API that allows developers to create applications to run ad-hoc queries -on massive datasets. These sample Java applications demonstrate how to access the BigQuery API using the Google Java API -Client Libraries. - -For more information, read the [Getting Started with BigQuery and the Google Java API Client -library][1] codelab. +Google's BigQuery Service features a REST-based API that allows developers to +create applications to run ad-hoc queries on massive datasets. These sample +Java applications demonstrate how to access the BigQuery API. ## API Libraries + +We provide samples for multiple methods of accessing the APIs in case you need +lower-level access, but the `cloud-client` samples are idiomatic and show the +recommended way to access the API. + - cloud-client (Preferred Option) - - This is Google Cloud's Official API Client, and the recommended way to interact with BQ + - This uses [Google Cloud Client + Libraries](http://googlecloudplatform.github.io/google-cloud-java/), and + the idiomatic and + [recommended](https://cloud.google.com/bigquery/docs/reference/libraries) + way to interact with BigQuery. - rest - - This shows java code implementing a sample client by making use of BQ's RESTful API. + - This uses BigQuery's RESTful API directly. Not recommended. - src - - This client was generated by running the veneer on the BQ protobuf definition. It demonstrates how you can build client libraries against our apiary service even if an official client library does not exist. + - This uses [Google API Client Libraries](https://developers.google.com/api-client-library/java/). Not recommended. ## Quickstart @@ -34,11 +40,6 @@ You can then run a given `ClassName` via: ## Language - [Java][3] -## Dependencies -- [Google APIs Client Library for Java][4] - -[1]: https://cloud.google.com/bigquery/bigquery-api-quickstart -[2]: https://developers.google.com/bigquery +[2]: https://cloud.google.com/bigquery [3]: https://java.com -[4]: http://code.google.com/p/google-api-java-client/ diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java index d8cfe398ba6..b71fc3adfc2 100644 --- a/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java @@ -21,12 +21,16 @@ import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.FieldValue; -import com.google.cloud.bigquery.QueryRequest; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobId; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryResponse; import com.google.cloud.bigquery.QueryResult; import java.util.Iterator; import java.util.List; +import java.util.UUID; // [END create_client] public class SimpleApp { @@ -35,18 +39,35 @@ public static void main(String... args) throws Exception { BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); // [END create_client] // [START run_query] - QueryRequest queryRequest = - QueryRequest - .newBuilder( - "SELECT " - + "APPROX_TOP_COUNT(corpus, 10) as title, " - + "COUNT(*) as unique_words " - + "FROM `publicdata.samples.shakespeare`;") + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder( + "SELECT " + + "APPROX_TOP_COUNT(corpus, 10) as title, " + + "COUNT(*) as unique_words " + + "FROM `publicdata.samples.shakespeare`;") // Use standard SQL syntax for queries. // See: https://cloud.google.com/bigquery/sql-reference/ .setUseLegacySql(false) .build(); - QueryResponse response = bigquery.query(queryRequest); + + // 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. + QueryResponse response = bigquery.getQueryResults(jobId); // [END run_query] // [START print_results]