2424
2525import java .io .IOException ;
2626import java .util .Iterator ;
27- import java .util .Scanner ;
2827
2928/**
3029 * Example of authorizing with BigQuery and reading from a public dataset.
3130 */
3231public class AsyncQuerySample {
32+ private static final String DEFAULT_QUERY =
33+ "SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;" ;
34+
3335 // [START main]
3436 /**
3537 * Prompts for all the parameters required to make a query.
@@ -39,17 +41,42 @@ public class AsyncQuerySample {
3941 * @throws InterruptedException InterruptedException
4042 */
4143 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 );
5380 while (pages .hasNext ()) {
5481 BigQueryUtils .printRows (pages .next ().getRows (), System .out );
5582 }
@@ -62,19 +89,24 @@ public static void main(final String[] args) throws IOException, InterruptedExce
6289 *
6390 * @param projectId Get this from Google Developers console
6491 * @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
6693 * @param waitTime How long to wait before retries
94+ * @param useLegacySql Boolean that is false if using standard SQL syntax.
6795 * @return An iterator to the result of your pages
6896 * @throws IOException Thrown if there's an IOException
6997 * @throws InterruptedException Thrown if there's an Interrupted Exception
7098 */
7199 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 )
73105 throws IOException , InterruptedException {
74106
75107 Bigquery bigquery = BigQueryServiceFactory .getService ();
76108
77- Job query = asyncQuery (bigquery , projectId , queryString , batch );
109+ Job query = asyncQuery (bigquery , projectId , queryString , useBatchMode , useLegacySql );
78110 Bigquery .Jobs .Get getRequest =
79111 bigquery .jobs ().get (projectId , query .getJobReference ().getJobId ());
80112
@@ -96,17 +128,27 @@ public static Iterator<GetQueryResultsResponse> run(
96128 * @param bigquery an authorized BigQuery client
97129 * @param projectId a String containing the project ID
98130 * @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.
100133 * @return a reference to the inserted query job
101134 * @throws IOException Thrown if there's a network exception
102135 */
103136 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 )
105142 throws IOException {
106143
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 );
108150
109- if (batch ) {
151+ if (useBatchMode ) {
110152 queryConfig .setPriority ("BATCH" );
111153 }
112154
0 commit comments