13
13
* License for the specific language governing permissions and limitations under
14
14
* the License.
15
15
*/
16
-
17
16
package com .google .cloud .bigquery .samples ;
18
17
19
-
20
18
import com .google .api .services .bigquery .Bigquery ;
21
19
import com .google .api .services .bigquery .Bigquery .Jobs .GetQueryResults ;
22
20
import com .google .api .services .bigquery .model .GetQueryResultsResponse ;
28
26
import java .util .Iterator ;
29
27
import java .util .Scanner ;
30
28
31
-
32
29
/**
33
30
* Example of authorizing with BigQuery and reading from a public dataset.
34
31
*/
35
- public class AsyncQuerySample extends BigqueryUtils {
32
+ public class AsyncQuerySample {
36
33
// [START main]
37
34
/**
38
35
* Prompts for all the parameters required to make a query.
@@ -41,23 +38,20 @@ public class AsyncQuerySample extends BigqueryUtils {
41
38
* @throws IOException IOException
42
39
* @throws InterruptedException InterruptedException
43
40
*/
44
- public static void main (final String [] args )
45
- throws IOException , InterruptedException {
41
+ public static void main (final String [] args ) throws IOException , InterruptedException {
46
42
Scanner scanner = new Scanner (System .in );
47
43
System .out .println ("Enter your project id: " );
48
44
String projectId = scanner .nextLine ();
49
45
System .out .println ("Enter your query string: " );
50
46
String queryString = scanner .nextLine ();
51
47
System .out .println ("Run query in batch mode? [true|false] " );
52
48
boolean batch = Boolean .valueOf (scanner .nextLine ());
53
- System .out .println ("Enter how often to check if your job is complete "
54
- + "(milliseconds): " );
49
+ System .out .println ("Enter how often to check if your job is complete " + "(milliseconds): " );
55
50
long waitTime = scanner .nextLong ();
56
51
scanner .close ();
57
- Iterator <GetQueryResultsResponse > pages = run (projectId , queryString ,
58
- batch , waitTime );
52
+ Iterator <GetQueryResultsResponse > pages = run (projectId , queryString , batch , waitTime );
59
53
while (pages .hasNext ()) {
60
- printRows (pages .next ().getRows (), System .out );
54
+ BigQueryUtils . printRows (pages .next ().getRows (), System .out );
61
55
}
62
56
}
63
57
// [END main]
@@ -70,58 +64,53 @@ public static void main(final String[] args)
70
64
* @param queryString Query we want to run against BigQuery
71
65
* @param batch True if you want to batch the queries
72
66
* @param waitTime How long to wait before retries
73
- * @return An interator to the result of your pages
67
+ * @return An iterator to the result of your pages
74
68
* @throws IOException Thrown if there's an IOException
75
69
* @throws InterruptedException Thrown if there's an Interrupted Exception
76
70
*/
77
- public static Iterator <GetQueryResultsResponse > run (final String projectId ,
78
- final String queryString ,
79
- final boolean batch ,
80
- final long waitTime )
71
+ public static Iterator <GetQueryResultsResponse > run (
72
+ final String projectId , final String queryString , final boolean batch , final long waitTime )
81
73
throws IOException , InterruptedException {
82
74
83
- Bigquery bigquery = BigqueryServiceFactory .getService ();
75
+ Bigquery bigquery = BigQueryServiceFactory .getService ();
84
76
85
77
Job query = asyncQuery (bigquery , projectId , queryString , batch );
86
- Bigquery .Jobs .Get getRequest = bigquery . jobs (). get (
87
- projectId , query .getJobReference ().getJobId ());
78
+ Bigquery .Jobs .Get getRequest =
79
+ bigquery . jobs (). get ( projectId , query .getJobReference ().getJobId ());
88
80
89
- //Poll every waitTime milliseconds,
90
- //retrying at most retries times if there are errors
91
- pollJob (getRequest , waitTime );
81
+ // Poll every waitTime milliseconds,
82
+ // retrying at most retries times if there are errors
83
+ BigQueryUtils . pollJob (getRequest , waitTime );
92
84
93
- GetQueryResults resultsRequest = bigquery . jobs (). getQueryResults (
94
- projectId , query .getJobReference ().getJobId ());
85
+ GetQueryResults resultsRequest =
86
+ bigquery . jobs (). getQueryResults ( projectId , query .getJobReference ().getJobId ());
95
87
96
- return getPages (resultsRequest );
88
+ return BigQueryUtils . getPages (resultsRequest );
97
89
}
98
90
// [END run]
99
91
100
92
// [START asyncQuery]
101
93
/**
102
94
* Inserts an asynchronous query Job for a particular query.
103
95
*
104
- * @param bigquery an authorized BigQuery client
96
+ * @param bigquery an authorized BigQuery client
105
97
* @param projectId a String containing the project ID
106
98
* @param querySql the actual query string
107
99
* @param batch True if you want to run the query as BATCH
108
100
* @return a reference to the inserted query job
109
101
* @throws IOException Thrown if there's a network exception
110
102
*/
111
- public static Job asyncQuery (final Bigquery bigquery ,
112
- final String projectId ,
113
- final String querySql ,
114
- final boolean batch ) throws IOException {
103
+ public static Job asyncQuery (
104
+ final Bigquery bigquery , final String projectId , final String querySql , final boolean batch )
105
+ throws IOException {
115
106
116
- JobConfigurationQuery queryConfig = new JobConfigurationQuery ()
117
- .setQuery (querySql );
107
+ JobConfigurationQuery queryConfig = new JobConfigurationQuery ().setQuery (querySql );
118
108
119
109
if (batch ) {
120
110
queryConfig .setPriority ("BATCH" );
121
111
}
122
112
123
- Job job = new Job ().setConfiguration (
124
- new JobConfiguration ().setQuery (queryConfig ));
113
+ Job job = new Job ().setConfiguration (new JobConfiguration ().setQuery (queryConfig ));
125
114
126
115
return bigquery .jobs ().insert (projectId , job ).execute ();
127
116
}
0 commit comments