Skip to content

Commit ace9bdb

Browse files
authored
Merge pull request #315 from GoogleCloudPlatform/tswast-bq-cloud-client
Add gcloud-java sample for BigQuery synchronous querying.
2 parents 63895f8 + f4fdfcd commit ace9bdb

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

bigquery/cloud-client/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Getting Started with BigQuery and the Google Java API Client library
2+
3+
[Google's BigQuery Service][BigQuery] features a REST-based API that allows
4+
developers to create applications to run ad-hoc queries on massive datasets.
5+
These sample Java applications demonstrate how to access the BigQuery API using
6+
the [Google Cloud Client Library for Java][google-cloud-java].
7+
8+
[BigQuery]: https://cloud.google.com/bigquery/
9+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/gcloud-java
10+
11+
## Quickstart
12+
13+
Install [Maven](http://maven.apache.org/).
14+
15+
Build your project with:
16+
17+
mvn clean package -DskipTests
18+
19+
You can then run a given `ClassName` via:
20+
21+
mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \
22+
-DpropertyName=propertyValue \
23+
-Dexec.args="any arguments to the app"
24+
25+
### Running a synchronous query
26+
27+
mvn exec:java -Dexec.mainClass=com.example.bigquery.SyncQuerySample \
28+
-Dquery='SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;' \
29+
-DuseLegacySql=false

bigquery/cloud-client/pom.xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
Copyright 2016 Google Inc. All Rights Reserved.
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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>come.example.bigquery</groupId>
19+
<artifactId>bigquery-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>gcloud-java</artifactId>
40+
<version>0.2.8</version>
41+
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.29</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.QueryRequest;
23+
import com.google.cloud.bigquery.QueryResponse;
24+
import com.google.cloud.bigquery.QueryResult;
25+
26+
import java.io.IOException;
27+
import java.io.PrintStream;
28+
import java.util.Iterator;
29+
import java.util.List;
30+
import java.util.stream.Collectors;
31+
32+
/**
33+
* Runs a synchronous query against BigQuery.
34+
*/
35+
public class SyncQuerySample {
36+
private static final String DEFAULT_QUERY =
37+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;";
38+
private static final long TEN_SECONDS_MILLIS = 10000;
39+
40+
/**
41+
* Prompts the user for the required parameters to perform a query.
42+
*/
43+
public static void main(final String[] args) throws IOException {
44+
String queryString = System.getProperty("query");
45+
if (queryString == null || queryString.isEmpty()) {
46+
System.out.println("The query property was not set, using default.");
47+
queryString = DEFAULT_QUERY;
48+
}
49+
System.out.printf("query: %s\n", queryString);
50+
51+
String waitTimeString = System.getProperty("waitTime");
52+
if (waitTimeString == null || waitTimeString.isEmpty()) {
53+
waitTimeString = "1000";
54+
}
55+
long waitTime = Long.parseLong(waitTimeString);
56+
System.out.printf("waitTime: %d (milliseconds)\n", waitTime);
57+
if (waitTime > TEN_SECONDS_MILLIS) {
58+
System.out.println(
59+
"WARNING: If the query is going to take longer than 10 seconds to complete, use an"
60+
+ " asynchronous query.");
61+
}
62+
63+
String useLegacySqlString = System.getProperty("useLegacySql");
64+
if (useLegacySqlString == null || useLegacySqlString.isEmpty()) {
65+
useLegacySqlString = "false";
66+
}
67+
boolean useLegacySql = Boolean.parseBoolean(useLegacySqlString);
68+
69+
run(System.out, queryString, waitTime, useLegacySql);
70+
}
71+
72+
/**
73+
* Perform the given query using the synchronous api.
74+
*
75+
* @param out stream to write results to
76+
* @param queryString query to run
77+
* @param waitTime Timeout in milliseconds before we abort
78+
* @param useLegacySql Boolean that is false if using standard SQL syntax.
79+
*/
80+
// [START run]
81+
public static void run(
82+
final PrintStream out,
83+
final String queryString,
84+
final long waitTime,
85+
final boolean useLegacySql) throws IOException {
86+
BigQuery bigquery =
87+
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.defaultInstance());
88+
89+
QueryRequest queryRequest =
90+
QueryRequest.builder(queryString)
91+
.maxWaitTime(waitTime)
92+
// Use standard SQL syntax or legacy SQL syntax for queries.
93+
// See: https://cloud.google.com/bigquery/sql-reference/
94+
.useLegacySql(useLegacySql)
95+
.build();
96+
QueryResponse response = bigquery.query(queryRequest);
97+
98+
if (response.hasErrors()) {
99+
throw new RuntimeException(
100+
response
101+
.executionErrors()
102+
.stream()
103+
.<String>map(err -> err.message())
104+
.collect(Collectors.joining("\n")));
105+
}
106+
107+
QueryResult result = response.result();
108+
Iterator<List<FieldValue>> iter = result.iterateAll();
109+
while (iter.hasNext()) {
110+
List<FieldValue> row = iter.next();
111+
out.println(row.stream().map(val -> val.toString()).collect(Collectors.joining(",")));
112+
}
113+
}
114+
// [END run]
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
25+
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.io.PrintStream;
29+
30+
/**
31+
* Tests for synchronous query sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
public class SyncQuerySampleTest {
35+
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
38+
@Before
39+
public void setUp() {
40+
bout = new ByteArrayOutputStream();
41+
out = new PrintStream(bout);
42+
}
43+
44+
@Test
45+
public void testSyncQuery() throws IOException {
46+
SyncQuerySample.run(
47+
out,
48+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;",
49+
10000 /* 10 seconds timeout */,
50+
false /* useLegacySql */);
51+
52+
String got = bout.toString();
53+
assertThat(got).contains("romeoandjuliet");
54+
}
55+
56+
@Test
57+
public void testSyncQueryLegacySql() throws IOException {
58+
SyncQuerySample.run(
59+
out,
60+
"SELECT corpus FROM [publicdata:samples.shakespeare] GROUP BY corpus;",
61+
10000 /* 10 seconds timeout */,
62+
true /* useLegacySql */);
63+
64+
String got = bout.toString();
65+
assertThat(got).contains("romeoandjuliet");
66+
}
67+
}

0 commit comments

Comments
 (0)