Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BQ: simple app sample, check for errors and use best practices. #574

Merged
merged 2 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions bigquery/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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/

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down