|
1 | 1 | /*
|
2 |
| - Copyright 2015, Google, Inc. |
3 |
| - Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| - you may not use this file except in compliance with the License. |
5 |
| - 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 |
10 |
| - distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| - See the License for the specific language governing permissions and |
| 2 | + Copyright 2015, Google, Inc. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + 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 |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
13 | 13 | limitations under the License.
|
14 | 14 | */
|
15 | 15 | package com.google.cloud.bigquery.samples;
|
|
24 | 24 | import java.util.Iterator;
|
25 | 25 | import java.util.Scanner;
|
26 | 26 | /**
|
27 |
| - * TODO: Insert description here. (generated by elibixby) |
| 27 | + * Runs a synchronous query against Bigtable. |
28 | 28 | */
|
29 |
| -public class SyncQuerySample extends BigqueryUtils{ |
| 29 | +public class SyncQuerySample { |
| 30 | + |
30 | 31 |
|
| 32 | + /** |
| 33 | + * Protected because this is a collection of static methods. |
| 34 | + */ |
| 35 | + protected SyncQuerySample() { |
| 36 | + |
| 37 | + } |
31 | 38 |
|
32 | 39 | //[START main]
|
33 | 40 | /**
|
34 |
| - * @param args |
35 |
| - * @throws IOException |
| 41 | + * @param args args |
| 42 | + * @throws IOException ioexceptino |
36 | 43 | */
|
37 |
| - public static void main(String[] args) |
38 |
| - throws IOException{ |
39 |
| - |
40 |
| - |
| 44 | + public static void main(final String[] args) |
| 45 | + throws IOException { |
41 | 46 | Scanner scanner = new Scanner(System.in);
|
42 | 47 | System.out.println("Enter your project id: ");
|
43 | 48 | String projectId = scanner.nextLine();
|
44 | 49 | System.out.println("Enter your query string: ");
|
45 | 50 | String queryString = scanner.nextLine();
|
46 |
| - System.out.println("Enter how long to wait for the query to complete (in milliseconds):\n " + |
47 |
| - "(if longer than 10 seconds, use an asynchronous query)"); |
| 51 | + System.out.println("Enter how long to wait for the query to complete" |
| 52 | + + " (in milliseconds):\n " |
| 53 | + + "(if longer than 10 seconds, use an asynchronous query)"); |
48 | 54 | long waitTime = scanner.nextLong();
|
49 | 55 | scanner.close();
|
50 |
| - Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, waitTime); |
51 |
| - while(pages.hasNext()){ |
52 |
| - printRows(pages.next().getRows(), System.out); |
| 56 | + Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, |
| 57 | + waitTime); |
| 58 | + while (pages.hasNext()) { |
| 59 | + BigqueryUtils.printRows(pages.next().getRows(), System.out); |
53 | 60 | }
|
54 | 61 | }
|
55 | 62 | // [END main]
|
56 |
| - |
57 | 63 |
|
| 64 | + |
| 65 | + /** |
| 66 | + * |
| 67 | + * @param projectId project id from developer console |
| 68 | + * @param queryString query to run |
| 69 | + * @param waitTime Timeout in milliseconds before we abort |
| 70 | + * @return Iterator that pages through the results of the query |
| 71 | + * @throws IOException ioexception |
| 72 | + */ |
58 | 73 | // [START run]
|
59 |
| - public static Iterator<GetQueryResultsResponse> run(String projectId, |
60 |
| - String queryString, |
61 |
| - long waitTime) throws IOException{ |
| 74 | + public static Iterator<GetQueryResultsResponse> run(final String projectId, |
| 75 | + final String queryString, |
| 76 | + final long waitTime) throws IOException { |
62 | 77 | Bigquery bigquery = BigqueryServiceFactory.getService();
|
63 | 78 | //Wait until query is done with 10 second timeout, at most 5 retries on error
|
64 | 79 | QueryResponse query = bigquery.jobs().query(
|
65 | 80 | projectId,
|
66 |
| - new QueryRequest().setTimeoutMs(waitTime).setQuery(queryString)).execute(); |
67 |
| - |
68 |
| - //Make a request to get the results of the query |
| 81 | + new QueryRequest().setTimeoutMs(waitTime).setQuery(queryString)) |
| 82 | + .execute(); |
| 83 | + |
| 84 | + //Make a request to get the results of the query |
69 | 85 | //(timeout is zero since job should be complete)
|
70 |
| - |
| 86 | + |
71 | 87 | GetQueryResults getRequest = bigquery.jobs().getQueryResults(
|
72 | 88 | query.getJobReference().getProjectId(),
|
73 | 89 | query.getJobReference().getJobId());
|
74 |
| - |
75 |
| - |
76 |
| - return getPages(getRequest); |
| 90 | + |
| 91 | + |
| 92 | + return BigqueryUtils.getPages(getRequest); |
77 | 93 | }
|
78 | 94 | // [END run]
|
79 |
| - |
| 95 | + |
80 | 96 |
|
81 | 97 | }
|
0 commit comments