Skip to content

Commit

Permalink
Add shakespeare sample for BigQuery. (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast authored and dpebot committed Oct 28, 2016
1 parent fa247e5 commit 2e3f285
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bigquery/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
@@ -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<List<FieldValue>> iter = result.iterateAll();

while (iter.hasNext()) {
List<FieldValue> row = iter.next();
List<FieldValue> titles = row.get(0).repeatedValue();
System.out.println("titles:");

for (FieldValue titleValue : titles) {
List<FieldValue> 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]

Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 2e3f285

Please sign in to comment.