1
1
/**
2
2
* Copyright (c) 2015 Google Inc.
3
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
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
5
+ * not use this file except in compliance with the License. You may obtain
6
+ * a copy of the License at
6
7
*
7
8
* http://www.apache.org/licenses/LICENSE-2.0
8
9
*
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
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ * License for the specific language governing permissions and limitations under
12
14
* the License.
13
15
*/
14
16
30
32
/**
31
33
* Example of authorizing with BigQuery and reading from a public dataset.
32
34
*/
33
- public class AsyncQuerySample extends BigqueryUtils {
35
+ public class AsyncQuerySample extends BigqueryUtils {
34
36
35
37
36
38
// [START main]
37
39
/**
38
- * @param args
39
- * @throws IOException
40
- * @throws InterruptedException
40
+ * @param args Command line args
41
+ * @throws IOException IOException
42
+ * @throws InterruptedException InterruptedException
41
43
*/
42
- public static void main (String [] args )
44
+ public static void main (final String [] args )
43
45
throws IOException , InterruptedException {
44
46
45
47
Scanner scanner = new Scanner (System .in );
@@ -49,23 +51,35 @@ public static void main(String[] args)
49
51
String queryString = scanner .nextLine ();
50
52
System .out .println ("Run query in batch mode? [true|false] " );
51
53
boolean batch = Boolean .valueOf (scanner .nextLine ());
52
- System .out .println ("Enter how often to check if your job is complete (milliseconds): " );
54
+ System .out .println ("Enter how often to check if your job is complete "
55
+ + "(milliseconds): " );
53
56
long waitTime = scanner .nextLong ();
54
57
scanner .close ();
55
- Iterator <GetQueryResultsResponse > pages = run (projectId , queryString , batch , waitTime );
56
- while (pages .hasNext ()){
58
+ Iterator <GetQueryResultsResponse > pages = run (projectId , queryString ,
59
+ batch , waitTime );
60
+ while (pages .hasNext ()) {
57
61
printRows (pages .next ().getRows (), System .out );
58
62
}
59
63
60
64
}
61
65
// [END main]
62
-
63
66
// [START run]
64
- public static Iterator <GetQueryResultsResponse > run (String projectId ,
65
- String queryString ,
66
- boolean batch ,
67
- long waitTime )
68
- throws IOException , InterruptedException {
67
+
68
+ /**
69
+ *
70
+ * @param projectId Get this from Google Developers console
71
+ * @param queryString Query we want to run against BigQuery
72
+ * @param batch True if you want to batch the queries
73
+ * @param waitTime How long to wait before retries
74
+ * @return An interator to the result of your pages
75
+ * @throws IOException Thrown if there's an IOException
76
+ * @throws InterruptedException Thrown if there's an Interrupted Exception
77
+ */
78
+ public static Iterator <GetQueryResultsResponse > run (final String projectId ,
79
+ final String queryString ,
80
+ final boolean batch ,
81
+ final long waitTime )
82
+ throws IOException , InterruptedException {
69
83
70
84
Bigquery bigquery = BigqueryServiceFactory .getService ();
71
85
@@ -86,28 +100,29 @@ public static Iterator<GetQueryResultsResponse> run(String projectId,
86
100
87
101
// [START asyncQuery]
88
102
/**
89
- * Inserts an asynchronous query Job for a particular query
103
+ * Inserts an asynchronous query Job for a particular query.
90
104
*
91
105
* @param bigquery an authorized BigQuery client
92
106
* @param projectId a String containing the project ID
93
107
* @param querySql the actual query string
108
+ * @param batch True if you want to run the query as BATCH
94
109
* @return a reference to the inserted query job
95
- * @throws IOException
110
+ * @throws IOException Thrown if there's a network exception
96
111
*/
97
- public static Job asyncQuery (Bigquery bigquery ,
98
- String projectId ,
99
- String querySql ,
100
- boolean batch ) throws IOException {
112
+ public static Job asyncQuery (final Bigquery bigquery ,
113
+ final String projectId ,
114
+ final String querySql ,
115
+ final boolean batch ) throws IOException {
101
116
102
- JobConfigurationQuery query_config = new JobConfigurationQuery ()
117
+ JobConfigurationQuery queryConfig = new JobConfigurationQuery ()
103
118
.setQuery (querySql );
104
119
105
- if (batch ){
106
- query_config .setPriority ("BATCH" );
120
+ if (batch ) {
121
+ queryConfig .setPriority ("BATCH" );
107
122
}
108
123
109
124
Job job = new Job ().setConfiguration (
110
- new JobConfiguration ().setQuery (query_config ));
125
+ new JobConfiguration ().setQuery (queryConfig ));
111
126
112
127
return bigquery .jobs ().insert (projectId , job ).execute ();
113
128
}
0 commit comments