Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions bigquery/cloud-client/async_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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__,
Expand Down