diff --git a/bigquery/cloud-client/README.md b/bigquery/cloud-client/README.md new file mode 100644 index 00000000000..1046596da9e --- /dev/null +++ b/bigquery/cloud-client/README.md @@ -0,0 +1,29 @@ +# Getting Started with BigQuery and the Google Java API Client library + +[Google's BigQuery Service][BigQuery] features a REST-based API that allows +developers to create applications to run ad-hoc queries on massive datasets. +These sample Java applications demonstrate how to access the BigQuery API using +the [Google Cloud Client Library for Java][google-cloud-java]. + +[BigQuery]: https://cloud.google.com/bigquery/ +[google-cloud-java]: https://github.com/GoogleCloudPlatform/gcloud-java + +## Quickstart + +Install [Maven](http://maven.apache.org/). + +Build your project with: + + mvn clean package -DskipTests + +You can then run a given `ClassName` via: + + mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \ + -DpropertyName=propertyValue \ + -Dexec.args="any arguments to the app" + +### Running a synchronous query + + mvn exec:java -Dexec.mainClass=com.example.bigquery.SyncQuerySample \ + -Dquery='SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;' \ + -DuseLegacySql=false diff --git a/bigquery/cloud-client/pom.xml b/bigquery/cloud-client/pom.xml new file mode 100644 index 00000000000..680e3436154 --- /dev/null +++ b/bigquery/cloud-client/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + come.example.bigquery + bigquery-google-cloud-samples + jar + + + + doc-samples + com.google.cloud + 1.0.0 + ../.. + + + + 1.8 + 1.8 + UTF-8 + + + + + com.google.cloud + gcloud-java + 0.2.8 + + + + + junit + junit + 4.12 + test + + + com.google.truth + truth + 0.29 + test + + + diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java new file mode 100644 index 00000000000..4ff9bf54c62 --- /dev/null +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java @@ -0,0 +1,115 @@ +/* + 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 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.io.IOException; +import java.io.PrintStream; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Runs a synchronous query against BigQuery. + */ +public class SyncQuerySample { + private static final String DEFAULT_QUERY = + "SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;"; + private static final long TEN_SECONDS_MILLIS = 10000; + + /** + * Prompts the user for the required parameters to perform a query. + */ + public static void main(final String[] args) throws IOException { + String queryString = System.getProperty("query"); + if (queryString == null || queryString.isEmpty()) { + System.out.println("The query property was not set, using default."); + queryString = DEFAULT_QUERY; + } + System.out.printf("query: %s\n", queryString); + + String waitTimeString = System.getProperty("waitTime"); + if (waitTimeString == null || waitTimeString.isEmpty()) { + waitTimeString = "1000"; + } + long waitTime = Long.parseLong(waitTimeString); + System.out.printf("waitTime: %d (milliseconds)\n", waitTime); + if (waitTime > TEN_SECONDS_MILLIS) { + System.out.println( + "WARNING: If the query is going to take longer than 10 seconds to complete, use an" + + " asynchronous query."); + } + + String useLegacySqlString = System.getProperty("useLegacySql"); + if (useLegacySqlString == null || useLegacySqlString.isEmpty()) { + useLegacySqlString = "false"; + } + boolean useLegacySql = Boolean.parseBoolean(useLegacySqlString); + + run(System.out, queryString, waitTime, useLegacySql); + } + + /** + * Perform the given query using the synchronous api. + * + * @param out stream to write results to + * @param queryString query to run + * @param waitTime Timeout in milliseconds before we abort + * @param useLegacySql Boolean that is false if using standard SQL syntax. + */ + // [START run] + public static void run( + final PrintStream out, + final String queryString, + final long waitTime, + final boolean useLegacySql) throws IOException { + BigQuery bigquery = + new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.defaultInstance()); + + QueryRequest queryRequest = + QueryRequest.builder(queryString) + .maxWaitTime(waitTime) + // Use standard SQL syntax or legacy SQL syntax for queries. + // See: https://cloud.google.com/bigquery/sql-reference/ + .useLegacySql(useLegacySql) + .build(); + QueryResponse response = bigquery.query(queryRequest); + + if (response.hasErrors()) { + throw new RuntimeException( + response + .executionErrors() + .stream() + .map(err -> err.message()) + .collect(Collectors.joining("\n"))); + } + + QueryResult result = response.result(); + Iterator> iter = result.iterateAll(); + while (iter.hasNext()) { + List row = iter.next(); + out.println(row.stream().map(val -> val.toString()).collect(Collectors.joining(","))); + } + } + // [END run] +} diff --git a/bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleTest.java b/bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleTest.java new file mode 100644 index 00000000000..f606f2d9937 --- /dev/null +++ b/bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleTest.java @@ -0,0 +1,67 @@ +/* + 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.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +/** + * Tests for synchronous query sample. + */ +@RunWith(JUnit4.class) +public class SyncQuerySampleTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + } + + @Test + public void testSyncQuery() throws IOException { + SyncQuerySample.run( + out, + "SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;", + 10000 /* 10 seconds timeout */, + false /* useLegacySql */); + + String got = bout.toString(); + assertThat(got).contains("romeoandjuliet"); + } + + @Test + public void testSyncQueryLegacySql() throws IOException { + SyncQuerySample.run( + out, + "SELECT corpus FROM [publicdata:samples.shakespeare] GROUP BY corpus;", + 10000 /* 10 seconds timeout */, + true /* useLegacySql */); + + String got = bout.toString(); + assertThat(got).contains("romeoandjuliet"); + } +}