@@ -121,7 +121,6 @@ def __init__(self, bq_service, project_id, swallow_results=True):
121121 self .cache = {}
122122
123123 def _submit_query_job (self , query_data ):
124-
125124 """ Submit a query job to BigQuery.
126125
127126 This is similar to BigQueryClient.query, but gives the user
@@ -172,7 +171,6 @@ def _submit_query_job(self, query_data):
172171 return job_id , [self ._transform_row (row , schema ) for row in rows ]
173172
174173 def _insert_job (self , body_object ):
175-
176174 """ Submit a job to BigQuery
177175
178176 Direct proxy to the insert() method of the offical BigQuery
@@ -243,9 +241,7 @@ def get_query_schema(self, job_id):
243241 A list of dictionaries that represent the schema.
244242 """
245243
246- job_collection = self .bigquery .jobs ()
247- query_reply = self ._get_query_results (
248- job_collection , self .project_id , job_id , offset = 0 , limit = 0 )
244+ query_reply = self .get_query_results (job_id , offset = 0 , limit = 0 )
249245
250246 if not query_reply ['jobComplete' ]:
251247 logging .warning ('BigQuery job %s not complete' % job_id )
@@ -289,38 +285,41 @@ def check_job(self, job_id):
289285 included in the query table if it has completed.
290286 """
291287
292- job_collection = self .bigquery .jobs ()
293- query_reply = self ._get_query_results (
294- job_collection , self .project_id , job_id , offset = 0 , limit = 0 )
288+ query_reply = self .get_query_results (job_id , offset = 0 , limit = 0 )
295289
296290 return (query_reply .get ('jobComplete' , False ),
297291 int (query_reply .get ('totalRows' , 0 )))
298292
299- def get_query_rows (self , job_id , offset = None , limit = None ):
293+ def get_query_rows (self , job_id , offset = None , limit = None , timeout = 0 ):
300294 """Retrieve a list of rows from a query table by job id.
301-
302295 Args:
303296 job_id: The job id that references a BigQuery query.
304297 offset: The offset of the rows to pull from BigQuery.
305298 limit: The number of rows to retrieve from a query table.
306-
299+ timeout: Timeout in seconds.
307300 Returns:
308301 A list of dictionaries that represent table rows.
309302 """
310303
311- job_collection = self .bigquery .jobs ()
312- query_reply = self ._get_query_results (
313- job_collection , self .project_id , job_id , offset = offset ,
314- limit = limit )
315-
304+ # Get query results
305+ query_reply = self .get_query_results (job_id , offset = offset , limit = limit , timeout = timeout )
316306 if not query_reply ['jobComplete' ]:
317307 logging .warning ('BigQuery job %s not complete' % job_id )
318308 raise UnfinishedQueryException ()
319309
320- schema = query_reply [' schema' ][ ' fields' ]
310+ schema = query_reply [" schema" ][ " fields" ]
321311 rows = query_reply .get ('rows' , [])
322-
323- return [self ._transform_row (row , schema ) for row in rows ]
312+ page_token = query_reply .get ("pageToken" )
313+ records = [self ._transform_row (row , schema ) for row in rows ]
314+
315+ # Append to records if there are multiple pages for query results
316+ while page_token :
317+ query_reply = self .get_query_results (job_id , offset = offset , limit = limit ,
318+ page_token = page_token , timeout = timeout )
319+ page_token = query_reply .get ("pageToken" )
320+ rows = query_reply .get ('rows' , [])
321+ records += [self ._transform_row (row , schema ) for row in rows ]
322+ return records
324323
325324 def check_table (self , dataset , table ):
326325 """Check to see if a table exists.
@@ -1039,27 +1038,26 @@ def _in_range(self, start_time, end_time, time):
10391038 time <= start_time <= time + ONE_MONTH or \
10401039 time <= end_time <= time + ONE_MONTH
10411040
1042- def _get_query_results (self , job_collection , project_id , job_id ,
1043- offset = None , limit = None ):
1041+ def get_query_results (self , job_id , offset = None , limit = None , page_token = None , timeout = 0 ):
10441042 """Execute the query job indicated by the given job id.
1045-
10461043 Args:
1047- job_collection: The collection the job belongs to.
1048- project_id: The project id of the table.
10491044 job_id: The job id of the query to check.
10501045 offset: The index the result set should start at.
10511046 limit: The maximum number of results to retrieve.
1052-
1047+ page_token: Page token, returned by a previous call, to request the next page of results.
1048+ timeout: Timeout in seconds.
10531049 Returns:
10541050 The query reply.
10551051 """
10561052
1053+ job_collection = self .bigquery .jobs ()
10571054 return job_collection .getQueryResults (
1058- projectId = project_id ,
1055+ projectId = self . project_id ,
10591056 jobId = job_id ,
10601057 startIndex = offset ,
10611058 maxResults = limit ,
1062- timeoutMs = 0 ).execute ()
1059+ pageToken = page_token ,
1060+ timeoutMs = timeout * 1000 ).execute ()
10631061
10641062 def _transform_row (self , row , schema ):
10651063 """Apply the given schema to the given BigQuery data row.
0 commit comments