2121import com .google .cloud .bigquery .FieldValue ;
2222import com .google .cloud .bigquery .QueryJobConfiguration ;
2323import com .google .cloud .bigquery .QueryParameterValue ;
24- import com .google .cloud .bigquery .QueryResponse ;
25- import com .google .cloud .bigquery .QueryResult ;
24+ import com .google .cloud .bigquery .TableResult ;
2625import java .io .IOException ;
2726import java .util .Arrays ;
2827import java .util .List ;
@@ -112,8 +111,7 @@ public static void main(final String[] args) throws IOException, InterruptedExce
112111 // [START bigquery_query_params]
113112 private static void runNamed (final String corpus , final long minWordCount )
114113 throws InterruptedException {
115- BigQuery bigquery =
116- new BigQueryOptions .DefaultBigqueryFactory ().create (BigQueryOptions .getDefaultInstance ());
114+ BigQuery bigquery = BigQueryOptions .getDefaultInstance ().getService ();
117115
118116 String queryString =
119117 "SELECT word, word_count\n "
@@ -131,25 +129,9 @@ private static void runNamed(final String corpus, final long minWordCount)
131129 .build ();
132130
133131 // Execute the query.
134- QueryResponse response = bigquery .query (queryRequest );
135-
136- // Wait for the job to finish (if the query takes more than 10 seconds to complete).
137- while (!response .jobCompleted ()) {
138- Thread .sleep (1000 );
139- response = bigquery .getQueryResults (response .getJobId ());
140- }
141-
142- // Check for errors.
143- if (response .hasErrors ()) {
144- String firstError = "" ;
145- if (response .getExecutionErrors ().size () != 0 ) {
146- firstError = response .getExecutionErrors ().get (0 ).getMessage ();
147- }
148- throw new RuntimeException (firstError );
149- }
132+ TableResult result = bigquery .query (queryRequest );
150133
151134 // Print all pages of the results.
152- QueryResult result = response .getResult ();
153135 while (result != null ) {
154136 for (List <FieldValue > row : result .iterateAll ()) {
155137 System .out .printf ("%s: %d\n " , row .get (0 ).getStringValue (), row .get (1 ).getLongValue ());
@@ -165,8 +147,7 @@ private static void runNamed(final String corpus, final long minWordCount)
165147 */
166148 // [START bigquery_query_params_arrays]
167149 private static void runArray (String gender , String [] states ) throws InterruptedException {
168- BigQuery bigquery =
169- new BigQueryOptions .DefaultBigqueryFactory ().create (BigQueryOptions .getDefaultInstance ());
150+ BigQuery bigquery = BigQueryOptions .getDefaultInstance ().getService ();
170151
171152 String queryString =
172153 "SELECT name, sum(number) as count\n "
@@ -186,25 +167,9 @@ private static void runArray(String gender, String[] states) throws InterruptedE
186167 .build ();
187168
188169 // Execute the query.
189- QueryResponse response = bigquery .query (queryRequest );
190-
191- // Wait for the job to finish (if the query takes more than 10 seconds to complete).
192- while (!response .jobCompleted ()) {
193- Thread .sleep (1000 );
194- response = bigquery .getQueryResults (response .getJobId ());
195- }
196-
197- // Check for errors.
198- if (response .hasErrors ()) {
199- String firstError = "" ;
200- if (response .getExecutionErrors ().size () != 0 ) {
201- firstError = response .getExecutionErrors ().get (0 ).getMessage ();
202- }
203- throw new RuntimeException (firstError );
204- }
170+ TableResult result = bigquery .query (queryRequest );
205171
206172 // Print all pages of the results.
207- QueryResult result = response .getResult ();
208173 while (result != null ) {
209174 for (List <FieldValue > row : result .iterateAll ()) {
210175 System .out .printf ("%s: %d\n " , row .get (0 ).getStringValue (), row .get (1 ).getLongValue ());
@@ -217,8 +182,7 @@ private static void runArray(String gender, String[] states) throws InterruptedE
217182
218183 // [START bigquery_query_params_timestamps]
219184 private static void runTimestamp () throws InterruptedException {
220- BigQuery bigquery =
221- new BigQueryOptions .DefaultBigqueryFactory ().create (BigQueryOptions .getDefaultInstance ());
185+ BigQuery bigquery = BigQueryOptions .getDefaultInstance ().getService ();
222186
223187 DateTime timestamp = new DateTime (2016 , 12 , 7 , 8 , 0 , 0 , DateTimeZone .UTC );
224188
@@ -236,25 +200,9 @@ private static void runTimestamp() throws InterruptedException {
236200 .build ();
237201
238202 // Execute the query.
239- QueryResponse response = bigquery .query (queryRequest );
240-
241- // Wait for the job to finish (if the query takes more than 10 seconds to complete).
242- while (!response .jobCompleted ()) {
243- Thread .sleep (1000 );
244- response = bigquery .getQueryResults (response .getJobId ());
245- }
246-
247- // Check for errors.
248- if (response .hasErrors ()) {
249- String firstError = "" ;
250- if (response .getExecutionErrors ().size () != 0 ) {
251- firstError = response .getExecutionErrors ().get (0 ).getMessage ();
252- }
253- throw new RuntimeException (firstError );
254- }
203+ TableResult result = bigquery .query (queryRequest );
255204
256205 // Print all pages of the results.
257- QueryResult result = response .getResult ();
258206 DateTimeFormatter formatter = ISODateTimeFormat .dateTimeNoMillis ().withZoneUTC ();
259207 while (result != null ) {
260208 for (List <FieldValue > row : result .iterateAll ()) {
0 commit comments