|
| 1 | +/* |
| 2 | + Copyright 2016, Google, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package com.example.bigquery; |
| 18 | + |
| 19 | +import com.google.cloud.bigquery.BigQuery; |
| 20 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 21 | +import com.google.cloud.bigquery.FieldValue; |
| 22 | +import com.google.cloud.bigquery.QueryRequest; |
| 23 | +import com.google.cloud.bigquery.QueryResponse; |
| 24 | +import com.google.cloud.bigquery.QueryResult; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.PrintStream; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.List; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +/** |
| 33 | + * Runs a synchronous query against BigQuery. |
| 34 | + */ |
| 35 | +public class SyncQuerySample { |
| 36 | + private static final String DEFAULT_QUERY = |
| 37 | + "SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;"; |
| 38 | + private static final long TEN_SECONDS_MILLIS = 10000; |
| 39 | + |
| 40 | + /** |
| 41 | + * Prompts the user for the required parameters to perform a query. |
| 42 | + */ |
| 43 | + public static void main(final String[] args) throws IOException { |
| 44 | + String queryString = System.getProperty("query"); |
| 45 | + if (queryString == null || queryString.isEmpty()) { |
| 46 | + System.out.println("The query property was not set, using default."); |
| 47 | + queryString = DEFAULT_QUERY; |
| 48 | + } |
| 49 | + System.out.printf("query: %s\n", queryString); |
| 50 | + |
| 51 | + String waitTimeString = System.getProperty("waitTime"); |
| 52 | + if (waitTimeString == null || waitTimeString.isEmpty()) { |
| 53 | + waitTimeString = "1000"; |
| 54 | + } |
| 55 | + long waitTime = Long.parseLong(waitTimeString); |
| 56 | + System.out.printf("waitTime: %d (milliseconds)\n", waitTime); |
| 57 | + if (waitTime > TEN_SECONDS_MILLIS) { |
| 58 | + System.out.println( |
| 59 | + "WARNING: If the query is going to take longer than 10 seconds to complete, use an" |
| 60 | + + " asynchronous query."); |
| 61 | + } |
| 62 | + |
| 63 | + String useLegacySqlString = System.getProperty("useLegacySql"); |
| 64 | + if (useLegacySqlString == null || useLegacySqlString.isEmpty()) { |
| 65 | + useLegacySqlString = "false"; |
| 66 | + } |
| 67 | + boolean useLegacySql = Boolean.parseBoolean(useLegacySqlString); |
| 68 | + |
| 69 | + run(System.out, queryString, waitTime, useLegacySql); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Perform the given query using the synchronous api. |
| 74 | + * |
| 75 | + * @param out stream to write results to |
| 76 | + * @param queryString query to run |
| 77 | + * @param waitTime Timeout in milliseconds before we abort |
| 78 | + * @param useLegacySql Boolean that is false if using standard SQL syntax. |
| 79 | + */ |
| 80 | + // [START run] |
| 81 | + public static void run( |
| 82 | + final PrintStream out, |
| 83 | + final String queryString, |
| 84 | + final long waitTime, |
| 85 | + final boolean useLegacySql) throws IOException { |
| 86 | + BigQuery bigquery = |
| 87 | + new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.defaultInstance()); |
| 88 | + |
| 89 | + QueryRequest queryRequest = |
| 90 | + QueryRequest.builder(queryString) |
| 91 | + .maxWaitTime(waitTime) |
| 92 | + // Use standard SQL syntax or legacy SQL syntax for queries. |
| 93 | + // See: https://cloud.google.com/bigquery/sql-reference/ |
| 94 | + .useLegacySql(useLegacySql) |
| 95 | + .build(); |
| 96 | + QueryResponse response = bigquery.query(queryRequest); |
| 97 | + |
| 98 | + if (response.hasErrors()) { |
| 99 | + throw new RuntimeException( |
| 100 | + response |
| 101 | + .executionErrors() |
| 102 | + .stream() |
| 103 | + .<String>map(err -> err.message()) |
| 104 | + .collect(Collectors.joining("\n"))); |
| 105 | + } |
| 106 | + |
| 107 | + QueryResult result = response.result(); |
| 108 | + Iterator<List<FieldValue>> iter = result.iterateAll(); |
| 109 | + while (iter.hasNext()) { |
| 110 | + List<FieldValue> row = iter.next(); |
| 111 | + out.println(row.stream().map(val -> val.toString()).collect(Collectors.joining(","))); |
| 112 | + } |
| 113 | + } |
| 114 | + // [END run] |
| 115 | +} |
0 commit comments