Skip to content

Commit 2999401

Browse files
authored
Merge pull request #574 from GoogleCloudPlatform/tswast-bq
BQ: simple app sample, check for errors and use best practices.
2 parents dc1a6c8 + 7a3767b commit 2999401

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

bigquery/README.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
# Getting Started with BigQuery and the Google Java API Client library
1+
# Getting Started with BigQuery
22

3-
Google's BigQuery Service features a REST-based API that allows developers to create applications to run ad-hoc queries
4-
on massive datasets. These sample Java applications demonstrate how to access the BigQuery API using the Google Java API
5-
Client Libraries.
6-
7-
For more information, read the [Getting Started with BigQuery and the Google Java API Client
8-
library][1] codelab.
3+
Google's BigQuery Service features a REST-based API that allows developers to
4+
create applications to run ad-hoc queries on massive datasets. These sample
5+
Java applications demonstrate how to access the BigQuery API.
96

107
## API Libraries
8+
9+
We provide samples for multiple methods of accessing the APIs in case you need
10+
lower-level access, but the `cloud-client` samples are idiomatic and show the
11+
recommended way to access the API.
12+
1113
- cloud-client (Preferred Option)
12-
- This is Google Cloud's Official API Client, and the recommended way to interact with BQ
14+
- This uses [Google Cloud Client
15+
Libraries](http://googlecloudplatform.github.io/google-cloud-java/), and
16+
the idiomatic and
17+
[recommended](https://cloud.google.com/bigquery/docs/reference/libraries)
18+
way to interact with BigQuery.
1319
- rest
14-
- This shows java code implementing a sample client by making use of BQ's RESTful API.
20+
- This uses BigQuery's RESTful API directly. Not recommended.
1521
- src
16-
- 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.
22+
- This uses [Google API Client Libraries](https://developers.google.com/api-client-library/java/). Not recommended.
1723

1824
## Quickstart
1925

@@ -34,11 +40,6 @@ You can then run a given `ClassName` via:
3440
## Language
3541
- [Java][3]
3642

37-
## Dependencies
38-
- [Google APIs Client Library for Java][4]
39-
40-
[1]: https://cloud.google.com/bigquery/bigquery-api-quickstart
41-
[2]: https://developers.google.com/bigquery
43+
[2]: https://cloud.google.com/bigquery
4244
[3]: https://java.com
43-
[4]: http://code.google.com/p/google-api-java-client/
4445

bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java

+30-9
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
import com.google.cloud.bigquery.BigQuery;
2222
import com.google.cloud.bigquery.BigQueryOptions;
2323
import com.google.cloud.bigquery.FieldValue;
24-
import com.google.cloud.bigquery.QueryRequest;
24+
import com.google.cloud.bigquery.Job;
25+
import com.google.cloud.bigquery.JobId;
26+
import com.google.cloud.bigquery.JobInfo;
27+
import com.google.cloud.bigquery.QueryJobConfiguration;
2528
import com.google.cloud.bigquery.QueryResponse;
2629
import com.google.cloud.bigquery.QueryResult;
2730

2831
import java.util.Iterator;
2932
import java.util.List;
33+
import java.util.UUID;
3034
// [END create_client]
3135

3236
public class SimpleApp {
@@ -35,18 +39,35 @@ public static void main(String... args) throws Exception {
3539
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
3640
// [END create_client]
3741
// [START run_query]
38-
QueryRequest queryRequest =
39-
QueryRequest
40-
.newBuilder(
41-
"SELECT "
42-
+ "APPROX_TOP_COUNT(corpus, 10) as title, "
43-
+ "COUNT(*) as unique_words "
44-
+ "FROM `publicdata.samples.shakespeare`;")
42+
QueryJobConfiguration queryConfig =
43+
QueryJobConfiguration.newBuilder(
44+
"SELECT "
45+
+ "APPROX_TOP_COUNT(corpus, 10) as title, "
46+
+ "COUNT(*) as unique_words "
47+
+ "FROM `publicdata.samples.shakespeare`;")
4548
// Use standard SQL syntax for queries.
4649
// See: https://cloud.google.com/bigquery/sql-reference/
4750
.setUseLegacySql(false)
4851
.build();
49-
QueryResponse response = bigquery.query(queryRequest);
52+
53+
// Create a job ID so that we can safely retry.
54+
JobId jobId = JobId.of(UUID.randomUUID().toString());
55+
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
56+
57+
// Wait for the query to complete.
58+
queryJob = queryJob.waitFor();
59+
60+
// Check for errors
61+
if (queryJob == null) {
62+
throw new RuntimeException("Job no longer exists");
63+
} else if (queryJob.getStatus().getError() != null) {
64+
// You can also look at queryJob.getStatus().getExecutionErrors() for all
65+
// errors, not just the latest one.
66+
throw new RuntimeException(queryJob.getStatus().getError().toString());
67+
}
68+
69+
// Get the results.
70+
QueryResponse response = bigquery.getQueryResults(jobId);
5071
// [END run_query]
5172

5273
// [START print_results]

0 commit comments

Comments
 (0)