From 32d233b3c0edd255f2fd603a58d74521802d76a9 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Wed, 5 Oct 2016 11:18:20 -0700 Subject: [PATCH] BigQuery - use new QueryJob.results() method. This method was added in https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2083. I can remove my hack now. --- bigquery/cloud-client/async_query.py | 30 ++++++++++------------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/bigquery/cloud-client/async_query.py b/bigquery/cloud-client/async_query.py index 37192d15659..2531c61528d 100755 --- a/bigquery/cloud-client/async_query.py +++ b/bigquery/cloud-client/async_query.py @@ -30,6 +30,16 @@ from google.cloud import bigquery +def wait_for_job(job): + while True: + job.reload() # Refreshes the state via a GET request. + if job.state == 'DONE': + if job.error_result: + raise RuntimeError(job.error_result) + return + time.sleep(1) + + def async_query(query): client = bigquery.Client() query_job = client.run_async_query(str(uuid.uuid4()), query) @@ -38,16 +48,8 @@ def async_query(query): wait_for_job(query_job) - # Manually construct the QueryResults. - # TODO: The client library will provide a helper method that does this. - # https://github.com/GoogleCloudPlatform/gcloud-python/issues/2083 - query_results = bigquery.query.QueryResults('', client) - query_results._properties['jobReference'] = { - 'jobId': query_job.name, - 'projectId': query_job.project - } - # Drain the query results by requesting a page at a time. + query_results = query_job.results() page_token = None while True: @@ -62,16 +64,6 @@ def async_query(query): break -def wait_for_job(job): - while True: - job.reload() # Refreshes the state via a GET request. - if job.state == 'DONE': - if job.error_result: - raise RuntimeError(job.error_result) - return - time.sleep(1) - - if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__,