|
| 1 | +Python Client for Google BigQuery |
| 2 | +================================= |
| 3 | + |
| 4 | + Python idiomatic client for `Google BigQuery`_ |
| 5 | + |
| 6 | +.. _Google BigQuery: https://cloud.google.com/bigquery/what-is-bigquery |
| 7 | + |
| 8 | +|pypi| |versions| |
| 9 | + |
| 10 | +- `Documentation`_ |
| 11 | + |
| 12 | +.. _Documentation: https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/usage.html |
| 13 | + |
| 14 | +Quick Start |
| 15 | +----------- |
| 16 | + |
| 17 | +.. code-block:: console |
| 18 | +
|
| 19 | + $ pip install --upgrade google-cloud-bigquery |
| 20 | +
|
| 21 | +Fore more information on setting up your Python development environment, such as installing ``pip`` and on your system, please refer to `Python Development Environment Setup Guide`_ for Google Cloud Platform. |
| 22 | + |
| 23 | +.. _Python Development Environment Setup Guide: https://cloud.google.com/python/setup |
| 24 | + |
| 25 | +Authentication |
| 26 | +-------------- |
| 27 | + |
| 28 | +With ``google-cloud-python`` we try to make authentication as painless as |
| 29 | +possible. Check out the `Authentication section`_ in our documentation to |
| 30 | +learn more. You may also find the `authentication document`_ shared by all |
| 31 | +the ``google-cloud-*`` libraries to be helpful. |
| 32 | + |
| 33 | +.. _Authentication section: https://google-cloud-python.readthedocs.io/en/latest/core/auth.html |
| 34 | +.. _authentication document: https://github.com/GoogleCloudPlatform/google-cloud-common/tree/master/authentication |
| 35 | + |
| 36 | +Using the API |
| 37 | +------------- |
| 38 | + |
| 39 | +Querying massive datasets can be time consuming and expensive without the |
| 40 | +right hardware and infrastructure. Google `BigQuery`_ (`BigQuery API docs`_) |
| 41 | +solves this problem by enabling super-fast, SQL queries against |
| 42 | +append-mostly tables, using the processing power of Google's infrastructure. |
| 43 | + |
| 44 | +.. _BigQuery: https://cloud.google.com/bigquery/what-is-bigquery |
| 45 | +.. _BigQuery API docs: https://cloud.google.com/bigquery/docs/reference/v2/ |
| 46 | + |
| 47 | +Create a dataset |
| 48 | +~~~~~~~~~~~~~~~~ |
| 49 | + |
| 50 | +.. code:: python |
| 51 | +
|
| 52 | + from google.cloud import bigquery |
| 53 | + from google.cloud.bigquery import Dataset |
| 54 | +
|
| 55 | + client = bigquery.Client() |
| 56 | +
|
| 57 | + dataset_ref = client.dataset('dataset_name') |
| 58 | + dataset = Dataset(dataset_ref) |
| 59 | + dataset.description = 'my dataset' |
| 60 | + dataset = client.create_dataset(dataset) # API request |
| 61 | +
|
| 62 | +Load data from CSV |
| 63 | +~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +.. code:: python |
| 66 | +
|
| 67 | + import csv |
| 68 | +
|
| 69 | + from google.cloud import bigquery |
| 70 | + from google.cloud.bigquery import LoadJobConfig |
| 71 | + from google.cloud.bigquery import SchemaField |
| 72 | +
|
| 73 | + client = bigquery.Client() |
| 74 | +
|
| 75 | + SCHEMA = [ |
| 76 | + SchemaField('full_name', 'STRING', mode='required'), |
| 77 | + SchemaField('age', 'INTEGER', mode='required'), |
| 78 | + ] |
| 79 | + table_ref = client.dataset('dataset_name').table('table_name') |
| 80 | +
|
| 81 | + load_config = LoadJobConfig() |
| 82 | + load_config.skip_leading_rows = 1 |
| 83 | + load_config.schema = SCHEMA |
| 84 | +
|
| 85 | + # Contents of csv_file.csv: |
| 86 | + # Name,Age |
| 87 | + # Tim,99 |
| 88 | + with open('csv_file.csv', 'rb') as readable: |
| 89 | + client.load_table_from_file( |
| 90 | + readable, table_ref, job_config=load_config) # API request |
| 91 | +
|
| 92 | +Perform a query |
| 93 | +~~~~~~~~~~~~~~~ |
| 94 | + |
| 95 | +.. code:: python |
| 96 | +
|
| 97 | + # Perform a query. |
| 98 | + QUERY = ( |
| 99 | + 'SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` ' |
| 100 | + 'WHERE state = "TX" ' |
| 101 | + 'LIMIT 100') |
| 102 | + query_job = client.query(QUERY) # API request |
| 103 | + rows = query_job.result() # Waits for query to finish |
| 104 | +
|
| 105 | + for row in rows: |
| 106 | + print(row.name) |
| 107 | +
|
| 108 | +
|
| 109 | +See the ``google-cloud-python`` API `BigQuery documentation`_ to learn how |
| 110 | +to connect to BigQuery using this Client Library. |
| 111 | + |
| 112 | +.. _BigQuery documentation: https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/usage.html |
| 113 | + |
| 114 | +.. |pypi| image:: https://img.shields.io/pypi/v/google-cloud-bigquery.svg |
| 115 | + :target: https://pypi.org/project/google-cloud-bigquery/ |
| 116 | +.. |versions| image:: https://img.shields.io/pypi/pyversions/google-cloud-bigquery.svg |
| 117 | + :target: https://pypi.org/project/google-cloud-bigquery/ |
0 commit comments