|
| 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 | +// [START all] |
| 20 | +// [START create_client] |
| 21 | +import com.google.cloud.bigquery.BigQuery; |
| 22 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 23 | +import com.google.cloud.bigquery.FieldValue; |
| 24 | +import com.google.cloud.bigquery.QueryRequest; |
| 25 | +import com.google.cloud.bigquery.QueryResponse; |
| 26 | +import com.google.cloud.bigquery.QueryResult; |
| 27 | + |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.List; |
| 30 | +// [END create_client] |
| 31 | + |
| 32 | +public class SimpleApp { |
| 33 | + public static void main(String... args) throws Exception { |
| 34 | + // [START create_client] |
| 35 | + BigQuery bigquery = BigQueryOptions.defaultInstance().service(); |
| 36 | + // [END create_client] |
| 37 | + // [START run_query] |
| 38 | + QueryRequest queryRequest = |
| 39 | + QueryRequest |
| 40 | + .builder( |
| 41 | + "SELECT " |
| 42 | + + "APPROX_TOP_COUNT(corpus, 10) as title, " |
| 43 | + + "COUNT(*) as unique_words " |
| 44 | + + "FROM `publicdata.samples.shakespeare`;") |
| 45 | + // Use standard SQL syntax for queries. |
| 46 | + // See: https://cloud.google.com/bigquery/sql-reference/ |
| 47 | + .useLegacySql(false) |
| 48 | + .build(); |
| 49 | + QueryResponse response = bigquery.query(queryRequest); |
| 50 | + // [END run_query] |
| 51 | + |
| 52 | + // [START print_results] |
| 53 | + QueryResult result = response.result(); |
| 54 | + |
| 55 | + while (result != null) { |
| 56 | + Iterator<List<FieldValue>> iter = result.iterateAll(); |
| 57 | + |
| 58 | + while (iter.hasNext()) { |
| 59 | + List<FieldValue> row = iter.next(); |
| 60 | + List<FieldValue> titles = row.get(0).repeatedValue(); |
| 61 | + System.out.println("titles:"); |
| 62 | + |
| 63 | + for (FieldValue titleValue : titles) { |
| 64 | + List<FieldValue> titleRecord = titleValue.recordValue(); |
| 65 | + String title = titleRecord.get(0).stringValue(); |
| 66 | + long uniqueWords = titleRecord.get(1).longValue(); |
| 67 | + System.out.printf("\t%s: %d\n", title, uniqueWords); |
| 68 | + } |
| 69 | + |
| 70 | + long uniqueWords = row.get(1).longValue(); |
| 71 | + System.out.printf("total unique words: %d\n", uniqueWords); |
| 72 | + } |
| 73 | + |
| 74 | + result = result.nextPage(); |
| 75 | + } |
| 76 | + // [END print_results] |
| 77 | + } |
| 78 | +} |
| 79 | +// [END all] |
| 80 | + |
0 commit comments