Skip to content

Commit 44824dd

Browse files
authored
Merge pull request #305 from GoogleCloudPlatform/tswast-bq-standard-sql
BigQuery query samples: command line parameters.
2 parents 74c2d8f + 2d7fb47 commit 44824dd

File tree

5 files changed

+162
-44
lines changed

5 files changed

+162
-44
lines changed

bigquery/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# Getting Started with BigQuery and the Google Java API Client library
22

3-
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.
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.
49

510
## Quickstart
611

712
Install [Maven](http://maven.apache.org/).
813

914
Build your project with:
1015

11-
mvn package -DskipTests
16+
mvn clean package -DskipTests
1217

1318
You can then run a given `ClassName` via:
1419

bigquery/src/main/java/com/google/cloud/bigquery/samples/AsyncQuerySample.java

+61-19
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
import java.io.IOException;
2626
import java.util.Iterator;
27-
import java.util.Scanner;
2827

2928
/**
3029
* Example of authorizing with BigQuery and reading from a public dataset.
3130
*/
3231
public class AsyncQuerySample {
32+
private static final String DEFAULT_QUERY =
33+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;";
34+
3335
// [START main]
3436
/**
3537
* Prompts for all the parameters required to make a query.
@@ -39,17 +41,42 @@ public class AsyncQuerySample {
3941
* @throws InterruptedException InterruptedException
4042
*/
4143
public static void main(final String[] args) throws IOException, InterruptedException {
42-
Scanner scanner = new Scanner(System.in);
43-
System.out.println("Enter your project id: ");
44-
String projectId = scanner.nextLine();
45-
System.out.println("Enter your query string: ");
46-
String queryString = scanner.nextLine();
47-
System.out.println("Run query in batch mode? [true|false] ");
48-
boolean batch = Boolean.valueOf(scanner.nextLine());
49-
System.out.println("Enter how often to check if your job is complete " + "(milliseconds): ");
50-
long waitTime = scanner.nextLong();
51-
scanner.close();
52-
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, batch, waitTime);
44+
String projectId = System.getProperty("projectId");
45+
if (projectId == null || projectId.isEmpty()) {
46+
System.err.println("The projectId property must be set.");
47+
System.exit(1);
48+
}
49+
System.out.printf("projectId: %s\n", projectId);
50+
51+
String queryString = System.getProperty("query");
52+
if (queryString == null || queryString.isEmpty()) {
53+
System.out.println("The query property was not set, using default.");
54+
queryString = DEFAULT_QUERY;
55+
}
56+
System.out.printf("query: %s\n", queryString);
57+
58+
String useBatchString = System.getProperty("useBatchMode");
59+
if (useBatchString == null || useBatchString.isEmpty()) {
60+
useBatchString = "false";
61+
}
62+
boolean useBatchMode = Boolean.parseBoolean(useBatchString);
63+
System.out.printf("useBatchMode: %b\n", useBatchMode);
64+
65+
String waitTimeString = System.getProperty("waitTime");
66+
if (waitTimeString == null || waitTimeString.isEmpty()) {
67+
waitTimeString = "1000";
68+
}
69+
long waitTime = Long.parseLong(waitTimeString);
70+
System.out.printf("waitTime: %d (milliseconds)\n", waitTime);
71+
72+
String useLegacySqlString = System.getProperty("useLegacySql");
73+
if (useLegacySqlString == null || useLegacySqlString.isEmpty()) {
74+
useLegacySqlString = "false";
75+
}
76+
boolean useLegacySql = Boolean.parseBoolean(useLegacySqlString);
77+
78+
Iterator<GetQueryResultsResponse> pages =
79+
run(projectId, queryString, useBatchMode, waitTime, useLegacySql);
5380
while (pages.hasNext()) {
5481
BigQueryUtils.printRows(pages.next().getRows(), System.out);
5582
}
@@ -62,19 +89,24 @@ public static void main(final String[] args) throws IOException, InterruptedExce
6289
*
6390
* @param projectId Get this from Google Developers console
6491
* @param queryString Query we want to run against BigQuery
65-
* @param batch True if you want to batch the queries
92+
* @param useBatchMode True if you want to batch the queries
6693
* @param waitTime How long to wait before retries
94+
* @param useLegacySql Boolean that is false if using standard SQL syntax.
6795
* @return An iterator to the result of your pages
6896
* @throws IOException Thrown if there's an IOException
6997
* @throws InterruptedException Thrown if there's an Interrupted Exception
7098
*/
7199
public static Iterator<GetQueryResultsResponse> run(
72-
final String projectId, final String queryString, final boolean batch, final long waitTime)
100+
final String projectId,
101+
final String queryString,
102+
final boolean useBatchMode,
103+
final long waitTime,
104+
final boolean useLegacySql)
73105
throws IOException, InterruptedException {
74106

75107
Bigquery bigquery = BigQueryServiceFactory.getService();
76108

77-
Job query = asyncQuery(bigquery, projectId, queryString, batch);
109+
Job query = asyncQuery(bigquery, projectId, queryString, useBatchMode, useLegacySql);
78110
Bigquery.Jobs.Get getRequest =
79111
bigquery.jobs().get(projectId, query.getJobReference().getJobId());
80112

@@ -96,17 +128,27 @@ public static Iterator<GetQueryResultsResponse> run(
96128
* @param bigquery an authorized BigQuery client
97129
* @param projectId a String containing the project ID
98130
* @param querySql the actual query string
99-
* @param batch True if you want to run the query as BATCH
131+
* @param useBatchMode True if you want to run the query as BATCH
132+
* @param useLegacySql Boolean that is false if using standard SQL syntax.
100133
* @return a reference to the inserted query job
101134
* @throws IOException Thrown if there's a network exception
102135
*/
103136
public static Job asyncQuery(
104-
final Bigquery bigquery, final String projectId, final String querySql, final boolean batch)
137+
final Bigquery bigquery,
138+
final String projectId,
139+
final String querySql,
140+
final boolean useBatchMode,
141+
final boolean useLegacySql)
105142
throws IOException {
106143

107-
JobConfigurationQuery queryConfig = new JobConfigurationQuery().setQuery(querySql);
144+
JobConfigurationQuery queryConfig =
145+
new JobConfigurationQuery()
146+
.setQuery(querySql)
147+
// Set the useLegacySql parameter to false to use standard SQL syntax. See:
148+
// https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
149+
.setUseLegacySql(useLegacySql);
108150

109-
if (batch) {
151+
if (useBatchMode) {
110152
queryConfig.setPriority("BATCH");
111153
}
112154

bigquery/src/main/java/com/google/cloud/bigquery/samples/SyncQuerySample.java

+53-19
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222

2323
import java.io.IOException;
2424
import java.util.Iterator;
25-
import java.util.Scanner;
25+
2626
/**
2727
* Runs a synchronous query against Bigtable.
2828
*/
2929
public class SyncQuerySample {
30+
private static final String DEFAULT_QUERY =
31+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;";
32+
private static final long TEN_SECONDS_MILLIS = 10000;
3033

3134
/**
3235
* Protected because this is a collection of static methods.
@@ -41,18 +44,39 @@ protected SyncQuerySample() {}
4144
* @throws IOException ioexceptino
4245
*/
4346
public static void main(final String[] args) throws IOException {
44-
Scanner scanner = new Scanner(System.in);
45-
System.out.println("Enter your project id: ");
46-
String projectId = scanner.nextLine();
47-
System.out.println("Enter your query string: ");
48-
String queryString = scanner.nextLine();
49-
System.out.println(
50-
"Enter how long to wait for the query to complete"
51-
+ " (in milliseconds):\n "
52-
+ "(if longer than 10 seconds, use an asynchronous query)");
53-
long waitTime = scanner.nextLong();
54-
scanner.close();
55-
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, waitTime);
47+
String projectId = System.getProperty("projectId");
48+
if (projectId == null || projectId.isEmpty()) {
49+
System.err.println("The projectId property must be set.");
50+
System.exit(1);
51+
}
52+
System.out.printf("projectId: %s\n", projectId);
53+
54+
String queryString = System.getProperty("query");
55+
if (queryString == null || queryString.isEmpty()) {
56+
System.out.println("The query property was not set, using default.");
57+
queryString = DEFAULT_QUERY;
58+
}
59+
System.out.printf("query: %s\n", queryString);
60+
61+
String waitTimeString = System.getProperty("waitTime");
62+
if (waitTimeString == null || waitTimeString.isEmpty()) {
63+
waitTimeString = "1000";
64+
}
65+
long waitTime = Long.parseLong(waitTimeString);
66+
System.out.printf("waitTime: %d (milliseconds)\n", waitTime);
67+
if (waitTime > TEN_SECONDS_MILLIS) {
68+
System.out.println(
69+
"WARNING: If the query is going to take longer than 10 seconds to complete, use an"
70+
+ " asynchronous query.");
71+
}
72+
73+
String useLegacySqlString = System.getProperty("useLegacySql");
74+
if (useLegacySqlString == null || useLegacySqlString.isEmpty()) {
75+
useLegacySqlString = "false";
76+
}
77+
boolean useLegacySql = Boolean.parseBoolean(useLegacySqlString);
78+
79+
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, waitTime, useLegacySql);
5680
while (pages.hasNext()) {
5781
BigQueryUtils.printRows(pages.next().getRows(), System.out);
5882
}
@@ -65,23 +89,33 @@ public static void main(final String[] args) throws IOException {
6589
* @param projectId project id from developer console
6690
* @param queryString query to run
6791
* @param waitTime Timeout in milliseconds before we abort
92+
* @param useLegacySql Boolean that is false if using standard SQL syntax.
6893
* @return Iterator that pages through the results of the query
6994
* @throws IOException ioexception
7095
*/
7196
// [START run]
7297
public static Iterator<GetQueryResultsResponse> run(
73-
final String projectId, final String queryString, final long waitTime) throws IOException {
98+
final String projectId,
99+
final String queryString,
100+
final long waitTime,
101+
final boolean useLegacySql) throws IOException {
74102
Bigquery bigquery = BigQueryServiceFactory.getService();
75-
//Wait until query is done with 10 second timeout, at most 5 retries on error
103+
104+
// Wait until query is done with `waitTime` millisecond timeout, at most 5 retries on error.
76105
QueryResponse query =
77106
bigquery
78107
.jobs()
79-
.query(projectId, new QueryRequest().setTimeoutMs(waitTime).setQuery(queryString))
108+
.query(
109+
projectId,
110+
new QueryRequest()
111+
.setTimeoutMs(waitTime)
112+
.setQuery(queryString)
113+
// Set the useLegacySql parameter to false to use standard SQL syntax. See:
114+
// https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
115+
.setUseLegacySql(useLegacySql))
80116
.execute();
81117

82-
//Make a request to get the results of the query
83-
//(timeout is zero since job should be complete)
84-
118+
// Make a request to get the results of the query.
85119
GetQueryResults getRequest =
86120
bigquery
87121
.jobs()

bigquery/src/test/java/com/google/cloud/bigquery/samples/test/AsyncQuerySampleTest.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,41 @@ public class AsyncQuerySampleTest {
3838
@Test
3939
public void testInteractive() throws IOException, InterruptedException {
4040
Iterator<GetQueryResultsResponse> pages =
41-
AsyncQuerySample.run(Constants.PROJECT_ID, Constants.QUERY, false, 5000);
41+
AsyncQuerySample.run(
42+
Constants.PROJECT_ID,
43+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;",
44+
false /* useBatchMode */,
45+
5000,
46+
false /* useLegacySql */);
4247
while (pages.hasNext()) {
4348
assertThat(pages.next().getRows()).isNotEmpty();
4449
}
4550
}
4651

4752
@Test
48-
@Ignore // Batches can take up to 3 hours to run, probably shouldn't use this
53+
public void testInteractiveLegacySql() throws IOException, InterruptedException {
54+
Iterator<GetQueryResultsResponse> pages =
55+
AsyncQuerySample.run(
56+
Constants.PROJECT_ID,
57+
Constants.QUERY,
58+
false /* useBatchMode */,
59+
5000,
60+
true /* useLegacySql */);
61+
while (pages.hasNext()) {
62+
assertThat(pages.next().getRows()).isNotEmpty();
63+
}
64+
}
65+
66+
@Test
67+
@Ignore // Batches can take up to 3 hours to run, don't run during the system tests.
4968
public void testBatch() throws IOException, InterruptedException {
5069
Iterator<GetQueryResultsResponse> pages =
51-
AsyncQuerySample.run(Constants.PROJECT_ID, Constants.QUERY, true, 5000);
70+
AsyncQuerySample.run(
71+
Constants.PROJECT_ID,
72+
Constants.QUERY,
73+
true /* useBatchMode */,
74+
5000,
75+
true /* useLegacySql */);
5276
while (pages.hasNext()) {
5377
assertThat(pages.next().getRows()).isNotEmpty();
5478
}

bigquery/src/test/java/com/google/cloud/bigquery/samples/test/SyncQuerySampleTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@ public class SyncQuerySampleTest {
3737
@Test
3838
public void testSyncQuery() throws IOException {
3939
Iterator<GetQueryResultsResponse> pages =
40-
SyncQuerySample.run(Constants.PROJECT_ID, Constants.QUERY, 10000);
40+
SyncQuerySample.run(
41+
Constants.PROJECT_ID,
42+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;",
43+
10000,
44+
false /* useLegacySql */);
45+
while (pages.hasNext()) {
46+
assertThat(pages.next().getRows()).isNotEmpty();
47+
}
48+
}
49+
50+
@Test
51+
public void testSyncQueryLegacySql() throws IOException {
52+
Iterator<GetQueryResultsResponse> pages =
53+
SyncQuerySample.run(Constants.PROJECT_ID, Constants.QUERY, 10000, true /* useLegacySql */);
4154
while (pages.hasNext()) {
4255
assertThat(pages.next().getRows()).isNotEmpty();
4356
}

0 commit comments

Comments
 (0)