Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shakespeare sample for BigQuery. #390

Merged
merged 1 commit into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you add a manifest line in your pom.xml so you don't need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an example of that?


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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL or GQL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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");
}
}