|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 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 bigquery_query_shortquery] |
| 20 | +import com.google.cloud.bigquery.BigQuery; |
| 21 | +import com.google.cloud.bigquery.BigQueryException; |
| 22 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 23 | +import com.google.cloud.bigquery.JobId; |
| 24 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 25 | +import com.google.cloud.bigquery.TableResult; |
| 26 | + |
| 27 | +// Sample demonstrating short mode query execution. |
| 28 | +// |
| 29 | +// While this feature is still in preview, it is controlled by |
| 30 | +// setting the environment variable QUERY_PREVIEW_ENABLED=TRUE |
| 31 | +// to request short mode execution. |
| 32 | +public class QueryShortMode { |
| 33 | + |
| 34 | + public static void main(String[] args) { |
| 35 | + String query = |
| 36 | + "SELECT name, gender, SUM(number) AS total FROM " |
| 37 | + + "bigquery-public-data.usa_names.usa_1910_2013 GROUP BY " |
| 38 | + + "name, gender ORDER BY total DESC LIMIT 10"; |
| 39 | + queryShortMode(query); |
| 40 | + } |
| 41 | + |
| 42 | + public static void queryShortMode(String query) { |
| 43 | + try { |
| 44 | + // Initialize client that will be used to send requests. This client only needs |
| 45 | + // to be created once, and can be reused for multiple requests. |
| 46 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 47 | + |
| 48 | + // Execute the query. The returned TableResult provides access information |
| 49 | + // about the query execution as well as query results. |
| 50 | + TableResult results = bigquery.query(QueryJobConfiguration.of(query)); |
| 51 | + |
| 52 | + JobId jobId = results.getJobId(); |
| 53 | + if (jobId != null) { |
| 54 | + System.out.println("Query was run with job state. Job ID: " + jobId.toString()); |
| 55 | + } else { |
| 56 | + System.out.println("Query was run in short mode. Query ID: " + results.getQueryId()); |
| 57 | + } |
| 58 | + |
| 59 | + // Print the results. |
| 60 | + results |
| 61 | + .iterateAll() |
| 62 | + .forEach( |
| 63 | + row -> { |
| 64 | + System.out.print("name:" + row.get("name").getStringValue()); |
| 65 | + System.out.print(", gender: " + row.get("gender").getStringValue()); |
| 66 | + System.out.print(", total: " + row.get("total").getLongValue()); |
| 67 | + System.out.println(); |
| 68 | + }); |
| 69 | + |
| 70 | + } catch (BigQueryException | InterruptedException e) { |
| 71 | + System.out.println("Query not performed \n" + e.toString()); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +// [END bigquery_query_shortquery] |
0 commit comments