Skip to content

Commit 2e3f285

Browse files
tswastdpebot
authored andcommitted
Add shakespeare sample for BigQuery. (GoogleCloudPlatform#390)
1 parent fa247e5 commit 2e3f285

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

bigquery/cloud-client/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ You can then run a given `ClassName` via:
3131
mvn exec:java -Dexec.mainClass=com.example.bigquery.SyncQuerySample \
3232
-Dquery='SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;' \
3333
-DuseLegacySql=false
34+
35+
### Running the simple app example
36+
37+
To run the example from the [simple app example
38+
documentation](https://cloud.google.com/bigquery/create-simple-app-api):
39+
40+
mvn exec:java -Dexec.mainClass=com.example.bigquery.SimpleApp
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
// [START all]
20+
// [START create_client]
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.FieldValue;
24+
import com.google.cloud.bigquery.QueryRequest;
25+
import com.google.cloud.bigquery.QueryResponse;
26+
import com.google.cloud.bigquery.QueryResult;
27+
28+
import java.util.Iterator;
29+
import java.util.List;
30+
// [END create_client]
31+
32+
public class SimpleApp {
33+
public static void main(String... args) throws Exception {
34+
// [START create_client]
35+
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
36+
// [END create_client]
37+
// [START run_query]
38+
QueryRequest queryRequest =
39+
QueryRequest
40+
.builder(
41+
"SELECT "
42+
+ "APPROX_TOP_COUNT(corpus, 10) as title, "
43+
+ "COUNT(*) as unique_words "
44+
+ "FROM `publicdata.samples.shakespeare`;")
45+
// Use standard SQL syntax for queries.
46+
// See: https://cloud.google.com/bigquery/sql-reference/
47+
.useLegacySql(false)
48+
.build();
49+
QueryResponse response = bigquery.query(queryRequest);
50+
// [END run_query]
51+
52+
// [START print_results]
53+
QueryResult result = response.result();
54+
55+
while (result != null) {
56+
Iterator<List<FieldValue>> iter = result.iterateAll();
57+
58+
while (iter.hasNext()) {
59+
List<FieldValue> row = iter.next();
60+
List<FieldValue> titles = row.get(0).repeatedValue();
61+
System.out.println("titles:");
62+
63+
for (FieldValue titleValue : titles) {
64+
List<FieldValue> titleRecord = titleValue.recordValue();
65+
String title = titleRecord.get(0).stringValue();
66+
long uniqueWords = titleRecord.get(1).longValue();
67+
System.out.printf("\t%s: %d\n", title, uniqueWords);
68+
}
69+
70+
long uniqueWords = row.get(1).longValue();
71+
System.out.printf("total unique words: %d\n", uniqueWords);
72+
}
73+
74+
result = result.nextPage();
75+
}
76+
// [END print_results]
77+
}
78+
}
79+
// [END all]
80+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/**
31+
* Tests for simple app sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class SimpleAppIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void testQuickstart() throws Exception {
53+
SimpleApp.main();
54+
String got = bout.toString();
55+
assertThat(got).contains("hamlet");
56+
}
57+
}

0 commit comments

Comments
 (0)