|
| 1 | +Python Client for Google Cloud BigQuery |
| 2 | +======================================= |
| 3 | + |
| 4 | + Python idiomatic client for `Google Cloud BigQuery`_ |
| 5 | + |
| 6 | +.. _Google Cloud BigQuery: https://cloud.google.com/bigquery/what-is-bigquery |
| 7 | + |
| 8 | +- `Homepage`_ |
| 9 | +- `API Documentation`_ |
| 10 | + |
| 11 | +.. _Homepage: https://googlecloudplatform.github.io/google-cloud-python/ |
| 12 | +.. _API Documentation: http://googlecloudplatform.github.io/google-cloud-python/ |
| 13 | + |
| 14 | +Quick Start |
| 15 | +----------- |
| 16 | + |
| 17 | +:: |
| 18 | + |
| 19 | + $ pip install --upgrade google-cloud-bigquery |
| 20 | + |
| 21 | +Authentication |
| 22 | +-------------- |
| 23 | + |
| 24 | +With ``google-cloud-python`` we try to make authentication as painless as |
| 25 | +possible. Check out the `Authentication section`_ in our documentation to |
| 26 | +learn more. You may also find the `authentication document`_ shared by all |
| 27 | +the ``google-cloud-*`` libraries to be helpful. |
| 28 | + |
| 29 | +.. _Authentication section: http://google-cloud-python.readthedocs.io/en/latest/google-cloud-auth.html |
| 30 | +.. _authentication document: https://github.com/GoogleCloudPlatform/gcloud-common/tree/master/authentication |
| 31 | + |
| 32 | +Using the API |
| 33 | +------------- |
| 34 | + |
| 35 | +Querying massive datasets can be time consuming and expensive without the |
| 36 | +right hardware and infrastructure. Google `BigQuery`_ (`BigQuery API docs`_) |
| 37 | +solves this problem by enabling super-fast, SQL-like queries against |
| 38 | +append-only tables, using the processing power of Google's infrastructure. |
| 39 | + |
| 40 | +.. _BigQuery: https://cloud.google.com/bigquery/what-is-bigquery |
| 41 | +.. _BigQuery API docs: https://cloud.google.com/bigquery/docs/reference/v2/ |
| 42 | + |
| 43 | +Load data from CSV |
| 44 | +~~~~~~~~~~~~~~~~~~ |
| 45 | + |
| 46 | +.. code:: python |
| 47 | +
|
| 48 | + import csv |
| 49 | +
|
| 50 | + from google.cloud import bigquery |
| 51 | + from google.cloud.bigquery import SchemaField |
| 52 | +
|
| 53 | + client = bigquery.Client() |
| 54 | +
|
| 55 | + dataset = client.dataset('dataset_name') |
| 56 | + dataset.create() # API request |
| 57 | +
|
| 58 | + SCHEMA = [ |
| 59 | + SchemaField('full_name', 'STRING', mode='required'), |
| 60 | + SchemaField('age', 'INTEGER', mode='required'), |
| 61 | + ] |
| 62 | + table = dataset.table('table_name', SCHEMA) |
| 63 | + table.create() |
| 64 | +
|
| 65 | + with open('csv_file', 'rb') as readable: |
| 66 | + table.upload_from_file( |
| 67 | + readable, source_format='CSV', skip_leading_rows=1) |
| 68 | +
|
| 69 | +Perform a synchronous query |
| 70 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 71 | + |
| 72 | +.. code:: python |
| 73 | +
|
| 74 | + # Perform a synchronous query. |
| 75 | + QUERY = ( |
| 76 | + 'SELECT name FROM [bigquery-public-data:usa_names.usa_1910_2013] ' |
| 77 | + 'WHERE state = "TX"') |
| 78 | + query = client.run_sync_query('%s LIMIT 100' % QUERY) |
| 79 | + query.timeout_ms = TIMEOUT_MS |
| 80 | + query.run() |
| 81 | +
|
| 82 | + for row in query.rows: |
| 83 | + print(row) |
| 84 | +
|
| 85 | +
|
| 86 | +See the ``google-cloud-python`` API `BigQuery documentation`_ to learn how |
| 87 | +to connect to BigQuery using this Client Library. |
| 88 | + |
| 89 | +.. _BigQuery documentation: https://googlecloudplatform.github.io/google-cloud-python/stable/bigquery-usage.html |
0 commit comments