|
| 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_script] |
| 20 | +import com.google.api.gax.paging.Page; |
| 21 | +import com.google.cloud.bigquery.BigQuery; |
| 22 | +import com.google.cloud.bigquery.BigQueryException; |
| 23 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 24 | +import com.google.cloud.bigquery.Job; |
| 25 | +import com.google.cloud.bigquery.JobInfo; |
| 26 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 27 | + |
| 28 | +// Sample to run query script. |
| 29 | +public class QueryScript { |
| 30 | + |
| 31 | + public static void runQueryScript() { |
| 32 | + String script = |
| 33 | + "-- Declare a variable to hold names as an array.\n" |
| 34 | + + "DECLARE top_names ARRAY<STRING>;\n" |
| 35 | + + "-- Build an array of the top 100 names from the year 2017.\n" |
| 36 | + + "SET top_names = (\n" |
| 37 | + + " SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)\n" |
| 38 | + + " FROM `bigquery-public-data`.usa_names.usa_1910_current\n" |
| 39 | + + " WHERE year = 2017\n" |
| 40 | + + ");\n" |
| 41 | + + "-- Which names appear as words in Shakespeare's plays?\n" |
| 42 | + + "SELECT\n" |
| 43 | + + " name AS shakespeare_name\n" |
| 44 | + + "FROM UNNEST(top_names) AS name\n" |
| 45 | + + "WHERE name IN (\n" |
| 46 | + + " SELECT word\n" |
| 47 | + + " FROM `bigquery-public-data`.samples.shakespeare\n" |
| 48 | + + ");"; |
| 49 | + queryScript(script); |
| 50 | + } |
| 51 | + |
| 52 | + public static void queryScript(String script) { |
| 53 | + try { |
| 54 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 55 | + // once, and can be reused for multiple requests. |
| 56 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 57 | + |
| 58 | + QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(script).build(); |
| 59 | + Job createJob = bigquery.create(JobInfo.of(queryConfig)); |
| 60 | + // Wait for the whole script to finish. |
| 61 | + JobInfo jobInfo = createJob.waitFor(); |
| 62 | + String parentJobId = jobInfo.getJobId().getJob(); |
| 63 | + |
| 64 | + // Fetch jobs created by the SQL script. |
| 65 | + Page<Job> childJobs = bigquery.listJobs(BigQuery.JobListOption.parentJobId(parentJobId)); |
| 66 | + childJobs |
| 67 | + .iterateAll() |
| 68 | + .forEach(job -> System.out.printf("Child Job Id: ", job.getJobId().getJob())); |
| 69 | + |
| 70 | + System.out.println("Query script performed successfully."); |
| 71 | + } catch (BigQueryException | InterruptedException e) { |
| 72 | + System.out.println("Query not performed \n" + e.toString()); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +// [END bigquery_query_script] |
0 commit comments