Skip to content

Commit 6ce03fc

Browse files
tswastplamut
authored andcommitted
BigQuery - use new QueryJob.results() method. [(googleapis#555)](GoogleCloudPlatform/python-docs-samples#555)
This method was added in googleapis/google-cloud-python#2083. I can remove my hack now.
1 parent 4e64955 commit 6ce03fc

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

samples/snippets/async_query.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
from google.cloud import bigquery
3131

3232

33+
def wait_for_job(job):
34+
while True:
35+
job.reload() # Refreshes the state via a GET request.
36+
if job.state == 'DONE':
37+
if job.error_result:
38+
raise RuntimeError(job.error_result)
39+
return
40+
time.sleep(1)
41+
42+
3343
def async_query(query):
3444
client = bigquery.Client()
3545
query_job = client.run_async_query(str(uuid.uuid4()), query)
@@ -38,16 +48,8 @@ def async_query(query):
3848

3949
wait_for_job(query_job)
4050

41-
# Manually construct the QueryResults.
42-
# TODO: The client library will provide a helper method that does this.
43-
# https://github.com/GoogleCloudPlatform/gcloud-python/issues/2083
44-
query_results = bigquery.query.QueryResults('', client)
45-
query_results._properties['jobReference'] = {
46-
'jobId': query_job.name,
47-
'projectId': query_job.project
48-
}
49-
5051
# Drain the query results by requesting a page at a time.
52+
query_results = query_job.results()
5153
page_token = None
5254

5355
while True:
@@ -62,16 +64,6 @@ def async_query(query):
6264
break
6365

6466

65-
def wait_for_job(job):
66-
while True:
67-
job.reload() # Refreshes the state via a GET request.
68-
if job.state == 'DONE':
69-
if job.error_result:
70-
raise RuntimeError(job.error_result)
71-
return
72-
time.sleep(1)
73-
74-
7567
if __name__ == '__main__':
7668
parser = argparse.ArgumentParser(
7769
description=__doc__,

0 commit comments

Comments
 (0)