|
| 1 | +/* |
| 2 | + * Copyright 2020 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_legacy] |
| 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.QueryJobConfiguration; |
| 24 | +import com.google.cloud.bigquery.TableResult; |
| 25 | + |
| 26 | +public class RunLegacyQuery { |
| 27 | + |
| 28 | + public static void runLegacyQuery() { |
| 29 | + try { |
| 30 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 31 | + // once, and can be reused for multiple requests. |
| 32 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 33 | + |
| 34 | + // To use legacy SQL syntax, set useLegacySql to true. |
| 35 | + String query = |
| 36 | + "SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;"; |
| 37 | + QueryJobConfiguration queryConfig = |
| 38 | + QueryJobConfiguration.newBuilder(query).setUseLegacySql(true).build(); |
| 39 | + |
| 40 | + // Execute the query. |
| 41 | + TableResult result = bigquery.query(queryConfig); |
| 42 | + |
| 43 | + // Print the results. |
| 44 | + result.iterateAll().forEach(rows -> rows.forEach(row -> System.out.println(row.getValue()))); |
| 45 | + |
| 46 | + System.out.println("Legacy query ran successfully"); |
| 47 | + } catch (BigQueryException | InterruptedException e) { |
| 48 | + System.out.println("Legacy query did not run \n" + e.toString()); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +// [END bigquery_query_legacy] |
0 commit comments