8000 Merge pull request #608 from GoogleCloudPlatform/tswast-bq · pablorodrigo/java-docs-samples@9179959 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9179959

Browse files
authored
Merge pull request GoogleCloudPlatform#608 from GoogleCloudPlatform/tswast-bq
BQ: remove API client lib samples. Use a job for queries.
2 parents 7949349 + 7572190 commit 9179959

33 files changed

+380
-2147
lines changed

bigquery/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ recommended way to access the API.
1818
way to interact with BigQuery.
1919
- rest
2020
- This uses BigQuery's RESTful API directly. Not recommended.
21-
- src
22-
- This uses [Google API Client Libraries](https://developers.google.com/api-client-library/java/). Not recommended.
2321

2422
## Quickstart
2523

bigquery/cloud-client/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ You can then run a given `ClassName` via:
2626

2727
mvn exec:java -Dexec.mainClass=com.example.bigquery.QuickstartSample
2828

29-
### Running a synchronous query
29+
### Running a query using standard SQL syntax
3030

31-
mvn exec:java -Dexec.mainClass=com.example.bigquery.SyncQuerySample \
32-
-Dquery='SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;' \
33-
-DuseLegacySql=false
31+
mvn exec:java -Dexec.mainClass=com.example.bigquery.QuerySample \
32+
-Dexec.args=' \
33+
--query="SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" \
34+
--runStandardSqlQuery'
3435

3536
### Running the simple app example
3637

bigquery/cloud-client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>google-cloud-bigquery</artifactId>
4040
<version>0.12.0-beta</version>
4141
</dependency>
42+
<dependency>
43+
<groupId>commons-cli</groupId>
44+
<artifactId>commons-cli</artifactId>
45+
<version>1.3.1</version>
46+
</dependency>
4247
<dependency>
4348
<groupId>joda-time</groupId>
4449
<artifactId>joda-time</artifactId>

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

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,38 @@
3333
import java.util.Iterator;
3434
import java.util.List;
3535

36-
/**
37-
* A sample that demonstrates use of query parameters.
38-
*/
36+
/** A sample that demonstrates use of query parameters. */
3937
public class QueryParametersSample {
4038
private static final int ERROR_CODE = 1;
4139

4240
private static void printUsage() {
4341
System.err.println("Usage:");
4442
System.err.printf(
4543
"\tmvn exec:java -Dexec.mainClass=%s -Dexec.args='%s'\n",
46-
QueryParametersSample.class.getCanonicalName(),
47-
"${sample}");
44+
QueryParametersSample.class.getCanonicalName(), "${sample}");
4845
System.err.println();
4946
System.err.println("${sample} can be one of: named, array, timestamp");
5047
System.err.println();
5148
System.err.println("Usage for ${sample}=named:");
5249
System.err.printf(
5350
"\tmvn exec:java -Dexec.mainClass=%s -Dexec.args='%s'\n",
54-
QueryParametersSample.class.getCanonicalName(),
55-
"named ${corpus} ${minWordCount}");
51+
QueryParametersSample.class.getCanonicalName(), "named ${corpus} ${minWordCount}");
5652
System.err.println();
5753
System.err.println("Usage for sample=array:");
5854
System.err.printf(
5955
"\tmvn exec:java -Dexec.mainClass=%s -Dexec.args='%s'\n",
60-
QueryParametersSample.class.getCanonicalName(),
61-
"array ${gender} ${states...}");
56+
QueryParametersSample.class.getCanonicalName(), "array ${gender} ${states...}");
6257
System.err.println();
6358
System.err.println("\twhere ${gender} can be on of: M, F");
6459
System.err.println(
6560
"\tand ${states} is any upper-case 2-letter code for U.S. a state, e.g. CA.");
6661
System.err.println();
6762
System.err.printf(
6863
"\t\tmvn exec:java -Dexec.mainClass=%s -Dexec.args='%s'\n",
69-
QueryParametersSample.class.getCanonicalName(),
70-
"array F MD WA");
64+
QueryParametersSample.class.getCanonicalName(), "array F MD WA");
7165
}
7266

73-
/**
74-
* Prompts the user for the required parameters to perform a query.
75-
*/
67+
/** Prompts the user for the required parameters to perform a query. */
7668
public static void main(final String[] args) throws IOException, InterruptedException {
7769
if (args.length < 1) {
7870
System.err.println("Expected first argument 'sample'");
@@ -125,11 +117,12 @@ private static void runNamed(final String corpus, final long minWordCount)
125117
BigQuery bigquery =
126118
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
127119

128-
String queryString = "SELECT word, word_count\n"
129-
+ "FROM `bigquery-public-data.samples.shakespeare`\n"
130-
+ "WHERE corpus = @corpus\n"
131-
+ "AND word_count >= @min_word_count\n"
132-
+ "ORDER BY word_count DESC";
120+
String queryString =
121+
"SELECT word, word_count\n"
122+
+ "FROM `bigquery-public-data.samples.shakespeare`\n"
123+
+ "WHERE corpus = @corpus\n"
124+
+ "AND word_count >= @min_word_count\n"
125+
+ "ORDER BY word_count DESC";
133126
QueryRequest queryRequest =
134127
QueryRequest.newBuilder(queryString)
135128
.addNamedParameter("corpus", QueryParameterValue.string(corpus))
@@ -161,10 +154,7 @@ private static void runNamed(final String corpus, final long minWordCount)
161154

162155
while (iter.hasNext()) {
163156
List<FieldValue> row = iter.next();
164-
System.out.printf(
165-
"%s: %d\n",
166-
row.get(0).getStringValue(),
167-
row.get(1).getLongValue());
157+
System.out.printf("%s: %d\n", row.get(0).getStringValue(), row.get(1).getLongValue());
168158
}
169159
}
170160
// [END bigquery_query_params]
@@ -173,24 +163,22 @@ private static void runNamed(final String corpus, final long minWordCount)
173163
* Query the baby names database to find the most popular names for a gender in a list of states.
174164
*/
175165
// [START bigquery_query_params_arrays]
176-
private static void runArray(String gender, String[] states)
177-
throws InterruptedException {
166+
private static void runArray(String gender, String[] states) throws InterruptedException {
178167
BigQuery bigquery =
179168
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
180169

181-
String queryString = "SELECT name, sum(number) as count\n"
182-
+ "FROM `bigquery-public-data.usa_names.usa_1910_2013`\n"
183-
+ "WHERE gender = @gender\n"
184-
+ "AND state IN UNNEST(@states)\n"
185-
+ "GROUP BY name\n"
186-
+ "ORDER BY count DESC\n"
187-
+ "LIMIT 10;";
170+
String queryString =
171+
"SELECT name, sum(number) as count\n"
172+
+ "FROM `bigquery-public-data.usa_names.usa_1910_2013`\n"
173+
+ "WHERE gender = @gender\n"
174+
+ "AND state IN UNNEST(@states)\n"
175+
+ "GROUP BY name\n"
176+
+ "ORDER BY count DESC\n"
177+
+ "LIMIT 10;";
188178
QueryRequest queryRequest =
189179
QueryRequest.newBuilder(queryString)
190180
.addNamedParameter("gender", QueryParameterValue.string(gender))
191-
.addNamedParameter(
192-
"states",
193-
QueryParameterValue.array(states, String.class))
181+
.addNamedParameter("states", QueryParameterValue.array(states, String.class))
194182
// Standard SQL syntax is required for parameterized queries.
195183
// See: https://cloud.google.com/bigquery/sql-reference/
196184
.setUseLegacySql(false)
@@ -272,8 +260,7 @@ private static void runTimestamp() throws InterruptedException {
272260
new DateTime(
273261
// Timestamp values are returned in microseconds since 1970-01-01T00:00:00 UTC,
274262
// but org.joda.time.DateTime constructor accepts times in milliseconds.
275-
row.get(0).getTimestampValue() / 1000,
276-
DateTimeZone.UTC)));
263+
row.get(0).getTimestampValue() / 1000, DateTimeZone.UTC)));
277264
}
278265
}
279266
// [END bigquery_query_params_timestamps]
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
Copyright 2016, Google, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryOptions;
21+
import com.google.cloud.bigquery.FieldValue;
22+
import com.google.cloud.bigquery.Job;
23+
import com.google.cloud.bigquery.JobId;
24+
import com.google.cloud.bigquery.JobInfo;
25+
import com.google.cloud.bigquery.QueryJobConfiguration;
26+
import com.google.cloud.bigquery.QueryResponse;
27+
import com.google.cloud.bigquery.QueryResult;
28+
import com.google.cloud.bigquery.TableId;
29+
import org.apache.commons.cli.CommandLine;
30+
import org.apache.commons.cli.CommandLineParser;
31+
import org.apache.commons.cli.DefaultParser;
32+
import org.apache.commons.cli.Option;
33+
import org.apache.commons.cli.OptionGroup;
34+
import org.apache.commons.cli.Options;
35+
import org.apache.commons.cli.ParseException;
36+
37+
import java.io.IOException;
38+
import java.util.Iterator;
39+
import java.util.List;
40+
import java.util.UUID;
41+
import java.util.concurrent.TimeoutException;
42+
43+
/** Runs a query against BigQuery. */
44+
public class QuerySample {
45+
// [START query_config_simple]
46+
public static void runSimpleQuery(String queryString)
47+
throws TimeoutException, InterruptedException {
48+
QueryJobConfiguration queryConfig =
49+
QueryJobConfiguration.newBuilder(queryString).build();
50+
51+
runQuery(queryConfig);
52+
}
53+
// [END query_config_simple]
54+
55+
// [START query_config_standard_sql]
56+
public static void runStandardSqlQuery(String queryString)
57+
throws TimeoutException, InterruptedException {
58+
QueryJobConfiguration queryConfig =
59+
QueryJobConfiguration.newBuilder(queryString)
60+
// To use standard SQL syntax, set useLegacySql to false.
61+
// See: https://cloud.google.com/bigquery/sql-reference/
62+
.setUseLegacySql(false)
63+
.build();
64+
65+
runQuery(queryConfig);
66+
}
67+
// [END query_config_standard_sql]
68+
69+
// [START query_config_permanent_table]
70+
public static void runQueryPermanentTable(
71+
String queryString,
72+
String destinationDataset,
73+
String destinationTable,
74+
boolean allowLargeResults) throws TimeoutException, InterruptedException {
75+
QueryJobConfiguration queryConfig =
76+
QueryJobConfiguration.newBuilder(queryString)
77+
// Save the results of the query to a permanent table.
78+
// See: https://cloud.google.com/bigquery/querying-data#permanent-table
79+
.setDestinationTable(TableId.of(destinationDataset, destinationTable))
80+
// Allow results larger than the maximum response size.
81+
// If true, a destination table must be set.
82+
// See: https://cloud.google.com/bigquery/querying-data#large-results
83+
.setAllowLargeResults(allowLargeResults)
84+
.build();
85+
86+
runQuery(queryConfig);
87+
}
88+
// [END query_config_permanent_table]
89+
90+
// [START query_config_cache]
91+
public static void runUncachedQuery(String queryString)
92+
throws TimeoutException, InterruptedException {
93+
QueryJobConfiguration queryConfig =
94+
QueryJobConfiguration.newBuilder(queryString)
95+
// Do not use the query cache. Force live query evaluation.
96+
// See: https://cloud.google.com/bigquery/querying-data#query-caching
97+
.setUseQueryCache(false)
98+
.build();
99+
100+
runQuery(queryConfig);
101+
}
102+
// [END query_config_cache]
103+
104+
// [START query_config_batch]
105+
public static void runBatchQuery(String queryString)
106+
throws TimeoutException, InterruptedException {
107+
QueryJobConfiguration queryConfig =
108+
QueryJobConfiguration.newBuilder(queryString)
109+
// Run at batch priority, which won't count toward concurrent rate
110+
// limit.
111+
// See: https://cloud.google.com/bigquery/querying-data#interactive-batch
112+
.setPriority(QueryJobConfiguration.Priority.BATCH)
113+
.build();
114+
115+
runQuery(queryConfig);
116+
}
117+
// [END query_config_batch]
118+
119+
120+
// [START run_query]
121+
public static void runQuery(QueryJobConfiguration queryConfig)
122+
throws TimeoutException, InterruptedException {
123+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
124+
125+
// Create a job ID so that we can safely retry.
126+
JobId jobId = JobId.of(UUID.randomUUID().toString());
127+
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
128+
129+
// Wait for the query to complete.
130+
queryJob = queryJob.waitFor();
131+
132+
// Check for errors
133+
if (queryJob == null) {
134+
throw new RuntimeException("Job no longer exists");
135+
} else if (queryJob.getStatus().getError() != null) {
136+
// You can also look at queryJob.getStatus().getExecutionErrors() for all
137+
// errors, not just the latest one.
138+
throw new RuntimeException(queryJob.getStatus().getError().toString());
139+
}
140+
141+
// Get the results.
142+
QueryResponse response = bigquery.getQueryResults(jobId);
143+
QueryResult result = response.getResult();
144+
145+
// Print all pages of the results.
146+
while (result != null) {
147+
if (response.hasErrors()) {
148+
String firstError = "";
149+
if (response.getExecutionErrors().size() != 0) {
150+
firstError = response.getExecutionErrors().get(0).getMessage();
151+
}
152+
throw new RuntimeException(firstError);
153+
}
154+
155+
Iterator<List<FieldValue>> iter = result.iterateAll();
156+
while (iter.hasNext()) {
157+
List<FieldValue> row = iter.next();
158+
for (FieldValue val : row) {
159+
System.out.printf("%s,", val.toString());
160+
}
161+
System.out.printf("\n");
162+
}
163+
164+
result = result.getNextPage();
165+
}
166+
}
167+
// [END run_query]
168+
169+
/** Prompts the user for the required parameters to perform a query. */
170+
public static void main(final String[] args)
171+
throws IOException, InterruptedException, TimeoutException, ParseException {
172+
Options options = new Options();
173+
174+
// Use an OptionsGroup to choose which sample to run.
175+
OptionGroup samples = new OptionGroup();
176+
samples.addOption(Option.builder().longOpt("runSimpleQuery").build());
177+
samples.addOption(Option.builder().longOpt("runStandardSqlQuery").build());
178+
samples.addOption(Option.builder().longOpt("runPermanentTableQuery").build());
179+
samples.addOption(Option.builder().longOpt("runUncachedQuery").build());
180+
samples.addOption(Option.builder().longOpt("runBatchQuery").build());
181+
samples.isRequired();
182+
options.addOptionGroup(samples);
183+
184+
options.addOption(Option.builder().longOpt("query").hasArg().required().build());
185+
options.addOption(Option.builder().longOpt("destDataset").hasArg().build());
186+
options.addOption(Option.builder().longOpt("destTable").hasArg().build());
187+
options.addOption(Option.builder().longOpt("allowLargeResults").build());
188+
189+
CommandLineParser parser = new DefaultParser();
190+
CommandLine cmd = parser.parse(options, args);
191+
192+
String query = cmd.getOptionValue("query");
193+
if (cmd.hasOption("runSimpleQuery")) {
194+
runSimpleQuery(query);
195+
} else if ( 10A29 cmd.hasOption("runStandardSqlQuery")) {
196+
runStandardSqlQuery(query);
197+
} else if (cmd.hasOption("runPermanentTableQuery")) {
198+
String destDataset = cmd.getOptionValue("destDataset");
199+
String destTable = cmd.getOptionValue("destTable");
200+
boolean allowLargeResults = cmd.hasOption("allowLargeResults");
201+
runQueryPermanentTable(query, destDataset, destTable, allowLargeResults);
202+
} else if (cmd.hasOption("runUncachedQuery")) {
203+
runUncachedQuery(query);
204+
} else if (cmd.hasOption("runBatchQuery")) {
205+
runBatchQuery(query);
206+
}
207+
}
208+
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public static void main(String... args) throws Exception {
4141
// [START run_query]
4242
QueryJobConfiguration queryConfig =
4343
QueryJobConfiguration.newBuilder(
44-
"SELECT "
45-
+ "APPROX_TOP_COUNT(corpus, 10) as title, "
46-
+ "COUNT(*) as unique_words "
47-
+ "FROM `publicdata.samples.shakespeare`;")
44+
"SELECT "
45+
+ "APPROX_TOP_COUNT(corpus, 10) as title, "
46+
+ "COUNT(*) as unique_words "
47+
+ "FROM `publicdata.samples.shakespeare`;")
4848
// Use standard SQL syntax for queries.
4949
// See: https://cloud.google.com/bigquery/sql-reference/
5050
.setUseLegacySql(false)
@@ -98,4 +98,3 @@ public static void main(String... args) throws Exception {
9898
}
9999
}
100100
// [END all]
101-

0 commit comments

Comments
 (0)
0