Python Client for Google BigQuery¶
Querying massive datasets can be time consuming and expensive without the right hardware and infrastructure. Google BigQuery solves this problem by enabling super-fast, SQL queries against append-mostly tables, using the processing power of Google’s infrastructure.
Quick Start¶
In order to use this library, you first need to go through the following steps:
Installation¶
Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.
With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.
Supported Python Versions¶
Python >= 3.7
Unsupported Python Versions¶
Python == 2.7, Python == 3.5, Python == 3.6.
The last version of this library compatible with Python 2.7 and 3.5 is google-cloud-bigquery==1.28.0.
Mac/Linux¶
pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-bigquery
Windows¶
pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-bigquery
Example Usage¶
Perform a query¶
from google.cloud import bigquery
client = bigquery.Client()
# Perform a query.
QUERY = (
'SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` '
'WHERE state = "TX" '
'LIMIT 100')
query_job = client.query(QUERY) # API request
rows = query_job.result() # Waits for query to finish
for row in rows:
print(row.name)
Instrumenting With OpenTelemetry¶
This application uses OpenTelemetry to output tracing data from API calls to BigQuery. To enable OpenTelemetry tracing in the BigQuery client the following PyPI packages need to be installed:
pip install google-cloud-bigquery[opentelemetry] opentelemetry-exporter-gcp-trace
After installation, OpenTelemetry can be used in the BigQuery client and in BigQuery jobs. First, however, an exporter must be specified for where the trace data will be outputted to. An example of this can be found here:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
tracer_provider = TracerProvider()
tracer_provider = BatchSpanProcessor(CloudTraceSpanExporter())
trace.set_tracer_provider(TracerProvider())
In this example all tracing data will be published to the Google Cloud Trace console. For more information on OpenTelemetry, please consult the OpenTelemetry documentation.
Note
Because the BigQuery client uses the third-party requests
library
by default and the BigQuery-Storage client uses grpcio
library,
both are safe to share instances across threads. In multiprocessing
scenarios, the best practice is to create client instances after
multiprocessing.Pool
or multiprocessing.Process
invokes
os.fork()
.
More Examples¶
API Reference¶
Migration Guide¶
See the guides below for instructions on migrating from older to newer major releases
of this library (from 1.x
to 2.x
, or from 2.x
to 3.x
).
- 3.0.0 Migration Guide
- New Required Dependencies
- Type Annotations
- Re-organized Types
- Issuing queries with
Client.create_job
preserves destination table - Changes to data types when reading a pandas DataFrame
- Changes to data types loading a pandas DataFrame
- Changes to
Model
,Client.get_model
,Client.update_model
, andClient.list_models
- Legacy Protocol Buffers Types
- 2.0.0 Migration Guide
Changelog¶
For a list of all google-cloud-bigquery
releases:
- Changelog
- 3.27.0 (2024-11-01)
- 3.26.0 (2024-09-25)
- 3.25.0 (2024-06-17)
- 3.24.0 (2024-06-04)
- 3.23.1 (2024-05-21)
- 3.23.0 (2024-05-16)
- 3.22.0 (2024-04-19)
- 3.21.0 (2024-04-18)
- 3.20.1 (2024-04-01)
- 3.20.0 (2024-03-27)
- 3.19.0 (2024-03-11)
- 3.18.0 (2024-02-29)
- 3.17.2 (2024-01-30)
- 3.17.1 (2024-01-24)
- 3.17.0 (2024-01-24)
- 3.16.0 (2024-01-12)
- 3.15.0 (2024-01-09)
- 3.14.1 (2023-12-13)
- 3.14.0 (2023-12-08)
- 3.13.0 (2023-10-30)
- 3.12.0 (2023-10-02)
- 3.11.4 (2023-07-19)
- 3.11.3 (2023-06-27)
- 3.11.2 (2023-06-21)
- 3.11.1 (2023-06-09)
- 3.11.0 (2023-06-01)
- 3.10.0 (2023-04-18)
- 3.9.0 (2023-03-28)
- 3.8.0 (2023-03-24)
- 3.7.0 (2023-03-06)
- 3.6.0 (2023-02-22)
- 3.5.0 (2023-01-31)
- 3.4.2 (2023-01-13)
- 3.4.1 (2022-12-09)
- 3.4.0 (2022-11-17)
- 3.3.6 (2022-11-02)
- 3.3.4 (2022-09-29)
- 3.3.3 (2022-09-28)
- 3.3.2 (2022-08-16)
- 3.3.1 (2022-08-09)
- 3.3.0 (2022-07-25)
- 3.2.0 (2022-06-06)
- 3.1.0 (2022-05-09)
- 3.0.1 (2022-03-30)
- 3.0.0 (2022-03-29)
- 2.34.3 (2022-03-29)
- 2.34.2 (2022-03-05)
- 2.34.1 (2022-03-02)
- 2.34.0 (2022-02-18)
- 2.33.0 (2022-02-16)
- 2.32.0 (2022-01-12)
- 2.31.0 (2021-11-24)
- 2.30.1 (2021-11-04)
- 2.30.0 (2021-11-03)
- 2.29.0 (2021-10-27)
- 2.28.1 (2021-10-07)
- 2.28.0 (2021-09-30)
- 2.27.1 (2021-09-27)
- 2.27.0 (2021-09-24)
- 2.26.0 (2021-09-01)
- 2.25.2 (2021-08-31)
- 2.25.1 (2021-08-25)
- 2.25.0 (2021-08-24)
- 2.24.1 (2021-08-13)
- 2.24.0 (2021-08-11)
- 2.23.3 (2021-08-06)
- 2.23.2 (2021-07-29)
- 2.23.1 (2021-07-28)
- 2.23.0 (2021-07-27)
- 2.22.1 (2021-07-22)
- 2.22.0 (2021-07-19)
- 2.21.0 (2021-07-12)
- 2.20.0 (2021-06-07)
- 2.19.0 (2021-06-06)
- 2.18.0 (2021-06-02)
- 2.17.0 (2021-05-21)
- 2.16.1 (2021-05-12)
- 2.16.0 (2021-05-05)
- 2.15.0 (2021-04-29)
- 2.14.0 (2021-04-26)
- 2.13.1 (2021-03-23)
- 2.13.0 (2021-03-22)
- 2.12.0 (2021-03-16)
- 2.11.0 (2021-03-09)
- 2.10.0 (2021-02-25)
- 2.9.0 (2021-02-18)
- 2.8.0 (2021-02-08)
- 2.7.0 (2021-01-27)
- 2.6.2 (2021-01-11)
- 2.6.1 (2020-12-09)
- 2.6.0 (2020-12-07)
- 2.5.0 (2020-12-02)
- 2.4.0 (2020-11-16)
- 2.3.1
- 2.3.0 (2020-11-04)
- 2.2.0 (2020-10-19)
- 2.1.0 (2020-10-08)
- 2.0.0
- 1.28.0 (2020-09-22)
- 1.27.2 (2020-08-18)
- 1.27.1 (2020-08-18)
- 1.27.0 (2020-08-15)
- 1.26.1 (2020-07-25)
- 1.26.0 (2020-07-20)
- 1.25.0 (2020-06-06)
- 1.24.0
- 1.23.1
- 1.23.0
- 1.22.0
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.1
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.32.0
- 0.31.0
- 0.30.0
- 0.29.0
- 0.28.0
- 0.27.0
- 0.26.0