From 2e3f285b7dccfd978b14033f9869e728e8c13e56 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Fri, 28 Oct 2016 12:31:46 -0700 Subject: [PATCH] Add shakespeare sample for BigQuery. (#390) --- bigquery/cloud-client/README.md | 8 ++ .../java/com/example/bigquery/SimpleApp.java | 80 +++++++++++++++++++ .../com/example/bigquery/SimpleAppIT.java | 57 +++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java create mode 100644 bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java diff --git a/bigquery/cloud-client/README.md b/bigquery/cloud-client/README.md index d067a8389d5..da277d3737e 100644 --- a/bigquery/cloud-client/README.md +++ b/bigquery/cloud-client/README.md @@ -31,3 +31,11 @@ You can then run a given `ClassName` via: mvn exec:java -Dexec.mainClass=com.example.bigquery.SyncQuerySample \ -Dquery='SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;' \ -DuseLegacySql=false + +### Running the simple app example + +To run the example from the [simple app example +documentation](https://cloud.google.com/bigquery/create-simple-app-api): + + mvn exec:java -Dexec.mainClass=com.example.bigquery.SimpleApp + diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java new file mode 100644 index 00000000000..4aa355d7baf --- /dev/null +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java @@ -0,0 +1,80 @@ +/* + Copyright 2016, Google, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.example.bigquery; + +// [START all] +// [START create_client] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.FieldValue; +import com.google.cloud.bigquery.QueryRequest; +import com.google.cloud.bigquery.QueryResponse; +import com.google.cloud.bigquery.QueryResult; + +import java.util.Iterator; +import java.util.List; +// [END create_client] + +public class SimpleApp { + public static void main(String... args) throws Exception { + // [START create_client] + BigQuery bigquery = BigQueryOptions.defaultInstance().service(); + // [END create_client] + // [START run_query] + QueryRequest queryRequest = + QueryRequest + .builder( + "SELECT " + + "APPROX_TOP_COUNT(corpus, 10) as title, " + + "COUNT(*) as unique_words " + + "FROM `publicdata.samples.shakespeare`;") + // Use standard SQL syntax for queries. + // See: https://cloud.google.com/bigquery/sql-reference/ + .useLegacySql(false) + .build(); + QueryResponse response = bigquery.query(queryRequest); + // [END run_query] + + // [START print_results] + QueryResult result = response.result(); + + while (result != null) { + Iterator> iter = result.iterateAll(); + + while (iter.hasNext()) { + List row = iter.next(); + List titles = row.get(0).repeatedValue(); + System.out.println("titles:"); + + for (FieldValue titleValue : titles) { + List titleRecord = titleValue.recordValue(); + String title = titleRecord.get(0).stringValue(); + long uniqueWords = titleRecord.get(1).longValue(); + System.out.printf("\t%s: %d\n", title, uniqueWords); + } + + long uniqueWords = row.get(1).longValue(); + System.out.printf("total unique words: %d\n", uniqueWords); + } + + result = result.nextPage(); + } + // [END print_results] + } +} +// [END all] + diff --git a/bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java b/bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java new file mode 100644 index 00000000000..689348d45a9 --- /dev/null +++ b/bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java @@ -0,0 +1,57 @@ +/* + Copyright 2016, Google, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +/** + * Tests for simple app sample. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SimpleAppIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testQuickstart() throws Exception { + SimpleApp.main(); + String got = bout.toString(); + assertThat(got).contains("hamlet"); + } +}