24
24
25
25
import java .io .IOException ;
26
26
import java .util .Iterator ;
27
- import java .util .Scanner ;
28
27
29
28
/**
30
29
* Example of authorizing with BigQuery and reading from a public dataset.
31
30
*/
32
31
public class AsyncQuerySample {
32
+ private static final String DEFAULT_QUERY =
33
+ "SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;" ;
34
+
33
35
// [START main]
34
36
/**
35
37
* Prompts for all the parameters required to make a query.
@@ -39,17 +41,42 @@ public class AsyncQuerySample {
39
41
* @throws InterruptedException InterruptedException
40
42
*/
41
43
public static void main (final String [] args ) throws IOException , InterruptedException {
42
- Scanner scanner = new Scanner (System .in );
43
- System .out .println ("Enter your project id: " );
44
- String projectId = scanner .nextLine ();
45
- System .out .println ("Enter your query string: " );
46
- String queryString = scanner .nextLine ();
47
- System .out .println ("Run query in batch mode? [true|false] " );
48
- boolean batch = Boolean .valueOf (scanner .nextLine ());
49
- System .out .println ("Enter how often to check if your job is complete " + "(milliseconds): " );
50
- long waitTime = scanner .nextLong ();
51
- scanner .close ();
52
- Iterator <GetQueryResultsResponse > pages = run (projectId , queryString , batch , waitTime );
44
+ String projectId = System .getProperty ("projectId" );
45
+ if (projectId == null || projectId .isEmpty ()) {
46
+ System .err .println ("The projectId property must be set." );
47
+ System .exit (1 );
48
+ }
49
+ System .out .printf ("projectId: %s\n " , projectId );
50
+
51
+ String queryString = System .getProperty ("query" );
52
+ if (queryString == null || queryString .isEmpty ()) {
53
+ System .out .println ("The query property was not set, using default." );
54
+ queryString = DEFAULT_QUERY ;
55
+ }
56
+ System .out .printf ("query: %s\n " , queryString );
57
+
58
+ String useBatchString = System .getProperty ("useBatchMode" );
59
+ if (useBatchString == null || useBatchString .isEmpty ()) {
60
+ useBatchString = "false" ;
61
+ }
62
+ boolean useBatchMode = Boolean .parseBoolean (useBatchString );
63
+ System .out .printf ("useBatchMode: %b\n " , useBatchMode );
64
+
65
+ String waitTimeString = System .getProperty ("waitTime" );
66
+ if (waitTimeString == null || waitTimeString .isEmpty ()) {
67
+ waitTimeString = "1000" ;
68
+ }
69
+ long waitTime = Long .parseLong (waitTimeString );
70
+ System .out .printf ("waitTime: %d (milliseconds)\n " , waitTime );
71
+
72
+ String useLegacySqlString = System .getProperty ("useLegacySql" );
73
+ if (useLegacySqlString == null || useLegacySqlString .isEmpty ()) {
74
+ useLegacySqlString = "false" ;
75
+ }
76
+ boolean useLegacySql = Boolean .parseBoolean (useLegacySqlString );
77
+
78
+ Iterator <GetQueryResultsResponse > pages =
79
+ run (projectId , queryString , useBatchMode , waitTime , useLegacySql );
53
80
while (pages .hasNext ()) {
54
81
BigQueryUtils .printRows (pages .next ().getRows (), System .out );
55
82
}
@@ -62,19 +89,24 @@ public static void main(final String[] args) throws IOException, InterruptedExce
62
89
*
63
90
* @param projectId Get this from Google Developers console
64
91
* @param queryString Query we want to run against BigQuery
65
- * @param batch True if you want to batch the queries
92
+ * @param useBatchMode True if you want to batch the queries
66
93
* @param waitTime How long to wait before retries
94
+ * @param useLegacySql Boolean that is false if using standard SQL syntax.
67
95
* @return An iterator to the result of your pages
68
96
* @throws IOException Thrown if there's an IOException
69
97
* @throws InterruptedException Thrown if there's an Interrupted Exception
70
98
*/
71
99
public static Iterator <GetQueryResultsResponse > run (
72
- final String projectId , final String queryString , final boolean batch , final long waitTime )
100
+ final String projectId ,
101
+ final String queryString ,
102
+ final boolean useBatchMode ,
103
+ final long waitTime ,
104
+ final boolean useLegacySql )
73
105
throws IOException , InterruptedException {
74
106
75
107
Bigquery bigquery = BigQueryServiceFactory .getService ();
76
108
77
- Job query = asyncQuery (bigquery , projectId , queryString , batch );
109
+ Job query = asyncQuery (bigquery , projectId , queryString , useBatchMode , useLegacySql );
78
110
Bigquery .Jobs .Get getRequest =
79
111
bigquery .jobs ().get (projectId , query .getJobReference ().getJobId ());
80
112
@@ -96,17 +128,27 @@ public static Iterator<GetQueryResultsResponse> run(
96
128
* @param bigquery an authorized BigQuery client
97
129
* @param projectId a String containing the project ID
98
130
* @param querySql the actual query string
99
- * @param batch True if you want to run the query as BATCH
131
+ * @param useBatchMode True if you want to run the query as BATCH
132
+ * @param useLegacySql Boolean that is false if using standard SQL syntax.
100
133
* @return a reference to the inserted query job
101
134
* @throws IOException Thrown if there's a network exception
102
135
*/
103
136
public static Job asyncQuery (
104
- final Bigquery bigquery , final String projectId , final String querySql , final boolean batch )
137
+ final Bigquery bigquery ,
138
+ final String projectId ,
139
+ final String querySql ,
140
+ final boolean useBatchMode ,
141
+ final boolean useLegacySql )
105
142
throws IOException {
106
143
107
- JobConfigurationQuery queryConfig = new JobConfigurationQuery ().setQuery (querySql );
144
+ JobConfigurationQuery queryConfig =
145
+ new JobConfigurationQuery ()
146
+ .setQuery (querySql )
147
+ // Set the useLegacySql parameter to false to use standard SQL syntax. See:
148
+ // https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
149
+ .setUseLegacySql (useLegacySql );
108
150
109
- if (batch ) {
151
+ if (useBatchMode ) {
110
152
queryConfig .setPriority ("BATCH" );
111
153
}
112
154
0 commit comments