2525
2626import argparse
2727import datetime
28- import time
2928import uuid
3029
3130from google .cloud import bigquery
3231import pytz
3332
3433
35- def wait_for_job (job ):
36- while True :
37- job .reload () # Refreshes the state via a GET request.
38- if job .state == 'DONE' :
39- if job .error_result :
40- raise RuntimeError (job .errors )
41- return
42- time .sleep (1 )
43-
44-
45- def print_results (query_results ):
46- """Print the rows in the query's results."""
47- rows = query_results .fetch_data (max_results = 10 )
48- for row in rows :
49- print (row )
50-
51-
5234def query_positional_params (corpus , min_word_count ):
5335 client = bigquery .Client ()
5436 query = """
@@ -73,10 +55,14 @@ def query_positional_params(corpus, min_word_count):
7355 # See: https://cloud.google.com/bigquery/sql-reference/
7456 query_job .use_legacy_sql = False
7557
76- # Start the query and wait for the job to complete.
7758 query_job .begin ()
78- wait_for_job (query_job )
79- print_results (query_job .results ())
59+ query_job .result () # Wait for job to complete
60+
61+ # Print the results.
62+ destination_table = query_job .destination
63+ destination_table .reload ()
64+ for row in destination_table .fetch_data ():
65+ print (row )
8066
8167
8268def query_named_params (corpus , min_word_count ):
@@ -97,10 +83,14 @@ def query_named_params(corpus, min_word_count):
9783 'min_word_count' , 'INT64' , min_word_count )))
9884 query_job .use_legacy_sql = False
9985
100- # Start the query and wait for the job to complete.
10186 query_job .begin ()
102- wait_for_job (query_job )
103- print_results (query_job .results ())
87+ query_job .result () # Wait for job to complete
88+
89+ # Print the results.
90+ destination_table = query_job .destination
91+ destination_table .reload ()
92+ for row in destination_table .fetch_data ():
93+ print (row )
10494
10595
10696def query_array_params (gender , states ):
@@ -122,10 +112,14 @@ def query_array_params(gender, states):
122112 bigquery .ArrayQueryParameter ('states' , 'STRING' , states )))
123113 query_job .use_legacy_sql = False
124114
125- # Start the query and wait for the job to complete.
126115 query_job .begin ()
127- wait_for_job (query_job )
128- print_results (query_job .results ())
116+ query_job .result () # Wait for job to complete
117+
118+ # Print the results.
119+ destination_table = query_job .destination
120+ destination_table .reload ()
121+ for row in destination_table .fetch_data ():
122+ print (row )
129123
130124
131125def query_timestamp_params (year , month , day , hour , minute ):
@@ -142,10 +136,14 @@ def query_timestamp_params(year, month, day, hour, minute):
142136 year , month , day , hour , minute , tzinfo = pytz .UTC ))])
143137 query_job .use_legacy_sql = False
144138
145- # Start the query and wait for the job to complete.
146139 query_job .begin ()
147- wait_for_job (query_job )
148- print_results (query_job .results ())
140+ query_job .result () # Wait for job to complete
141+
142+ # Print the results.
143+ destination_table = query_job .destination
144+ destination_table .reload ()
145+ for row in destination_table .fetch_data ():
146+ print (row )
149147
150148
151149def query_struct_params (x , y ):
@@ -161,10 +159,14 @@ def query_struct_params(x, y):
161159 bigquery .ScalarQueryParameter ('y' , 'STRING' , y ))])
162160 query_job .use_legacy_sql = False
163161
164- # Start the query and wait for the job to complete.
165162 query_job .begin ()
166- wait_for_job (query_job )
167- print_results (query_job .results ())
163+ query_job .result () # Wait for job to complete
164+
165+ # Print the results.
166+ destination_table = query_job .destination
167+ destination_table .reload ()
168+ for row in destination_table .fetch_data ():
169+ print (row )
168170
169171
170172if __name__ == '__main__' :
0 commit comments