Skip to content

Commit

Permalink
Merge pull request #315 from GoogleCloudPlatform/tswast-bq-cloud-client
Browse files Browse the repository at this point in the history
Add gcloud-java sample for BigQuery synchronous querying.
  • Loading branch information
tswast committed Aug 29, 2016
2 parents 63895f8 + f4fdfcd commit ace9bdb
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 0 deletions.
29 changes: 29 additions & 0 deletions bigquery/cloud-client/README.md
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions bigquery/cloud-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!--
Copyright 2016 Google Inc. All Rights Reserved.
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>come.example.bigquery</groupId>
<artifactId>bigquery-google-cloud-samples</artifactId>
<packaging>jar</packaging>

<!-- Parent defines config for testing & linting. -->
<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.2.8</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.29</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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()
.<String>map(err -> err.message())
.collect(Collectors.joining("\n")));
}

QueryResult result = response.result();
Iterator<List<FieldValue>> iter = result.iterateAll();
while (iter.hasNext()) {
List<FieldValue> row = iter.next();
out.println(row.stream().map(val -> val.toString()).collect(Collectors.joining(",")));
}
}
// [END run]
}
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit ace9bdb

Please sign in to comment.