|
| 1 | +Python Client for Google BigQuery |
| 2 | +================================= |
| 3 | + |
| 4 | +|GA| |pypi| |versions| |
| 5 | + |
| 6 | +Querying massive datasets can be time consuming and expensive without the |
| 7 | +right hardware and infrastructure. Google `BigQuery`_ solves this problem by |
| 8 | +enabling super-fast, SQL queries against append-mostly tables, using the |
| 9 | +processing power of Google's infrastructure. |
| 10 | + |
| 11 | +- `Client Library Documentation`_ |
| 12 | +- `Product Documentation`_ |
| 13 | + |
| 14 | +.. |GA| image:: https://img.shields.io/badge/support-GA-gold.svg |
| 15 | + :target: https://github.com/googleapis/google-cloud-python/blob/main/README.rst#general-availability |
| 16 | +.. |pypi| image:: https://img.shields.io/pypi/v/google-cloud-bigquery.svg |
| 17 | + :target: https://pypi.org/project/google-cloud-bigquery/ |
| 18 | +.. |versions| image:: https://img.shields.io/pypi/pyversions/google-cloud-bigquery.svg |
| 19 | + :target: https://pypi.org/project/google-cloud-bigquery/ |
| 20 | +.. _BigQuery: https://cloud.google.com/bigquery/what-is-bigquery |
| 21 | +.. _Client Library Documentation: https://cloud.google.com/python/docs/reference/bigquery/latest/summary_overview |
| 22 | +.. _Product Documentation: https://cloud.google.com/bigquery/docs/reference/v2/ |
| 23 | + |
| 24 | +Quick Start |
| 25 | +----------- |
| 26 | + |
| 27 | +In order to use this library, you first need to go through the following steps: |
| 28 | + |
| 29 | +1. `Select or create a Cloud Platform project.`_ |
| 30 | +2. `Enable billing for your project.`_ |
| 31 | +3. `Enable the Google Cloud BigQuery API.`_ |
| 32 | +4. `Setup Authentication.`_ |
| 33 | + |
| 34 | +.. _Select or create a Cloud Platform project.: https://console.cloud.google.com/project |
| 35 | +.. _Enable billing for your project.: https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project |
| 36 | +.. _Enable the Google Cloud BigQuery API.: https://cloud.google.com/bigquery |
| 37 | +.. _Setup Authentication.: https://googleapis.dev/python/google-api-core/latest/auth.html |
| 38 | + |
| 39 | +Installation |
| 40 | +~~~~~~~~~~~~ |
| 41 | + |
| 42 | +Install this library in a `virtualenv`_ using pip. `virtualenv`_ is a tool to |
| 43 | +create isolated Python environments. The basic problem it addresses is one of |
| 44 | +dependencies and versions, and indirectly permissions. |
| 45 | + |
| 46 | +With `virtualenv`_, it's possible to install this library without needing system |
| 47 | +install permissions, and without clashing with the installed system |
| 48 | +dependencies. |
| 49 | + |
| 50 | +.. _`virtualenv`: https://virtualenv.pypa.io/en/latest/ |
| 51 | + |
| 52 | + |
| 53 | +Supported Python Versions |
| 54 | +^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 55 | +Python >= 3.9 |
| 56 | + |
| 57 | +Unsupported Python Versions |
| 58 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 59 | +Python == 2.7, Python == 3.5, Python == 3.6, Python == 3.7, and Python == 3.8. |
| 60 | + |
| 61 | +The last version of this library compatible with Python 2.7 and 3.5 is |
| 62 | +`google-cloud-bigquery==1.28.0`. |
| 63 | + |
| 64 | + |
| 65 | +Mac/Linux |
| 66 | +^^^^^^^^^ |
| 67 | + |
| 68 | +.. code-block:: console |
| 69 | +
|
| 70 | + pip install virtualenv |
| 71 | + virtualenv <your-env> |
| 72 | + source <your-env>/bin/activate |
| 73 | + <your-env>/bin/pip install google-cloud-bigquery |
| 74 | +
|
| 75 | +
|
| 76 | +Windows |
| 77 | +^^^^^^^ |
| 78 | + |
| 79 | +.. code-block:: console |
| 80 | +
|
| 81 | + pip install virtualenv |
| 82 | + virtualenv <your-env> |
| 83 | + <your-env>\Scripts\activate |
| 84 | + <your-env>\Scripts\pip.exe install google-cloud-bigquery |
| 85 | +
|
| 86 | +Example Usage |
| 87 | +------------- |
| 88 | + |
| 89 | +Perform a query |
| 90 | +~~~~~~~~~~~~~~~ |
| 91 | + |
| 92 | +.. code:: python |
| 93 | +
|
| 94 | + from google.cloud import bigquery |
| 95 | +
|
| 96 | + client = bigquery.Client() |
| 97 | +
|
| 98 | + # Perform a query. |
| 99 | + QUERY = ( |
| 100 | + 'SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` ' |
| 101 | + 'WHERE state = "TX" ' |
| 102 | + 'LIMIT 100') |
| 103 | + query_job = client.query(QUERY) # API request |
| 104 | + rows = query_job.result() # Waits for query to finish |
| 105 | +
|
| 106 | + for row in rows: |
| 107 | + print(row.name) |
| 108 | +
|
| 109 | +Instrumenting With OpenTelemetry |
| 110 | +-------------------------------- |
| 111 | + |
| 112 | +This application uses `OpenTelemetry`_ to output tracing data from |
| 113 | +API calls to BigQuery. To enable OpenTelemetry tracing in |
| 114 | +the BigQuery client the following PyPI packages need to be installed: |
| 115 | + |
| 116 | +.. _OpenTelemetry: https://opentelemetry.io |
| 117 | + |
| 118 | +.. code-block:: console |
| 119 | +
|
| 120 | + pip install google-cloud-bigquery[opentelemetry] opentelemetry-exporter-gcp-trace |
| 121 | +
|
| 122 | +After installation, OpenTelemetry can be used in the BigQuery |
| 123 | +client and in BigQuery jobs. First, however, an exporter must be |
| 124 | +specified for where the trace data will be outputted to. An |
| 125 | +example of this can be found here: |
| 126 | + |
| 127 | +.. code-block:: python |
| 128 | +
|
| 129 | + from opentelemetry import trace |
| 130 | + from opentelemetry.sdk.trace import TracerProvider |
| 131 | + from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 132 | + from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter |
| 133 | + tracer_provider = TracerProvider() |
| 134 | + tracer_provider = BatchSpanProcessor(CloudTraceSpanExporter()) |
| 135 | + trace.set_tracer_provider(TracerProvider()) |
| 136 | +
|
| 137 | +In this example all tracing data will be published to the Google |
| 138 | +`Cloud Trace`_ console. For more information on OpenTelemetry, please consult the `OpenTelemetry documentation`_. |
| 139 | + |
| 140 | +.. _OpenTelemetry documentation: https://opentelemetry-python.readthedocs.io |
| 141 | +.. _Cloud Trace: https://cloud.google.com/trace |
0 commit comments