|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Command-line application to perform asynchronous queries in BigQuery. |
| 18 | +
|
| 19 | +For more information, see the README.md under /bigquery. |
| 20 | +
|
| 21 | +Example invocation: |
| 22 | + $ python async_query.py \ |
| 23 | + 'SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus' |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse |
| 27 | +import time |
| 28 | +import uuid |
| 29 | + |
| 30 | +from gcloud import bigquery |
| 31 | + |
| 32 | + |
| 33 | +def async_query(query): |
| 34 | + client = bigquery.Client() |
| 35 | + query_job = client.run_async_query(str(uuid.uuid4()), query) |
| 36 | + query_job.use_legacy_sql = False |
| 37 | + query_job.begin() |
| 38 | + |
| 39 | + wait_for_job(query_job) |
| 40 | + |
| 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 | + |
| 50 | + # Drain the query results by requesting a page at a time. |
| 51 | + page_token = None |
| 52 | + |
| 53 | + while True: |
| 54 | + rows, total_rows, page_token = query_results.fetch_data( |
| 55 | + max_results=10, |
| 56 | + page_token=page_token) |
| 57 | + |
| 58 | + for row in rows: |
| 59 | + print(row) |
| 60 | + |
| 61 | + if not page_token: |
| 62 | + break |
| 63 | + |
| 64 | + |
| 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 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description=__doc__, |
| 78 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 79 | + parser.add_argument('query', help='BigQuery SQL Query.') |
| 80 | + |
| 81 | + args = parser.parse_args() |
| 82 | + |
| 83 | + async_query(args.query) |
0 commit comments