|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.cloud.bigquery.samples; |
| 16 | + |
| 17 | + |
| 18 | +import com.google.api.services.bigquery.Bigquery; |
| 19 | +import com.google.api.services.bigquery.Bigquery.Jobs.GetQueryResults; |
| 20 | +import com.google.api.services.bigquery.model.GetQueryResultsResponse; |
| 21 | +import com.google.api.services.bigquery.model.Job; |
| 22 | +import com.google.api.services.bigquery.model.JobConfiguration; |
| 23 | +import com.google.api.services.bigquery.model.JobConfigurationQuery; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.Scanner; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Example of authorizing with BigQuery and reading from a public dataset. |
| 32 | + */ |
| 33 | +public class AsyncQuerySample extends BigqueryUtils{ |
| 34 | + |
| 35 | + |
| 36 | + // [START main] |
| 37 | + /** |
| 38 | + * @param args |
| 39 | + * @throws IOException |
| 40 | + * @throws InterruptedException |
| 41 | + */ |
| 42 | + public static void main(String[] args) |
| 43 | + throws IOException, InterruptedException { |
| 44 | + |
| 45 | + Scanner scanner = new Scanner(System.in); |
| 46 | + System.out.println("Enter your project id: "); |
| 47 | + String projectId = scanner.nextLine(); |
| 48 | + System.out.println("Enter your query string: "); |
| 49 | + String queryString = scanner.nextLine(); |
| 50 | + System.out.println("Run query in batch mode? [true|false] "); |
| 51 | + boolean batch = Boolean.valueOf(scanner.nextLine()); |
| 52 | + System.out.println("Enter how often to check if your job is complete (milliseconds): "); |
| 53 | + long waitTime = scanner.nextLong(); |
| 54 | + scanner.close(); |
| 55 | + Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, batch, waitTime); |
| 56 | + while(pages.hasNext()){ |
| 57 | + printRows(pages.next().getRows(), System.out); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + // [END main] |
| 62 | + |
| 63 | + // [START run] |
| 64 | + public static Iterator<GetQueryResultsResponse> run(String projectId, |
| 65 | + String queryString, |
| 66 | + boolean batch, |
| 67 | + long waitTime) |
| 68 | + throws IOException, InterruptedException{ |
| 69 | + |
| 70 | + Bigquery bigquery = BigqueryServiceFactory.getService(); |
| 71 | + |
| 72 | + Job query = asyncQuery(bigquery, projectId, queryString, batch); |
| 73 | + Bigquery.Jobs.Get getRequest = bigquery.jobs().get( |
| 74 | + projectId, query.getJobReference().getJobId()); |
| 75 | + |
| 76 | + //Poll every waitTime milliseconds, |
| 77 | + //retrying at most retries times if there are errors |
| 78 | + pollJob(getRequest, waitTime); |
| 79 | + |
| 80 | + GetQueryResults resultsRequest = bigquery.jobs().getQueryResults( |
| 81 | + projectId, query.getJobReference().getJobId()); |
| 82 | + |
| 83 | + return getPages(resultsRequest); |
| 84 | + } |
| 85 | + // [END run] |
| 86 | + |
| 87 | + // [START asyncQuery] |
| 88 | + /** |
| 89 | + * Inserts an asynchronous query Job for a particular query |
| 90 | + * |
| 91 | + * @param bigquery an authorized BigQuery client |
| 92 | + * @param projectId a String containing the project ID |
| 93 | + * @param querySql the actual query string |
| 94 | + * @return a reference to the inserted query job |
| 95 | + * @throws IOException |
| 96 | + */ |
| 97 | + public static Job asyncQuery(Bigquery bigquery, |
| 98 | + String projectId, |
| 99 | + String querySql, |
| 100 | + boolean batch) throws IOException { |
| 101 | + |
| 102 | + JobConfigurationQuery query_config = new JobConfigurationQuery() |
| 103 | + .setQuery(querySql); |
| 104 | + |
| 105 | + if(batch){ |
| 106 | + query_config.setPriority("BATCH"); |
| 107 | + } |
| 108 | + |
| 109 | + Job job = new Job().setConfiguration( |
| 110 | + new JobConfiguration().setQuery(query_config)); |
| 111 | + |
| 112 | + return bigquery.jobs().insert(projectId, job).execute(); |
| 113 | + } |
| 114 | + // [END asyncQuery] |
| 115 | + |
| 116 | +} |
0 commit comments