Skip to main content

Google BigQuery boiler plate code

Project description

BigQuery 101

A small, opinionated convenience layer over Google BigQuery that cuts the boilerplate out of the common tasks: run a query into a polars DataFrame, and load a DataFrame back into a table. Data transfer uses Arrow/Parquet under the hood.

Install

uv add bigquery101
# or
pip install bigquery101

Requires Python ≥ 3.12.

Authentication

get_bigquery_client(project_id) creates a client using whatever credentials are available:

  • If GOOGLE_APPLICATION_CREDENTIALS points at a service-account JSON file, that file is used.
  • Otherwise Application Default Credentials are used (e.g. after gcloud auth application-default login).

Usage

Query into a DataFrame

import bigquery101

client = bigquery101.get_bigquery_client('my-project')

df = bigquery101.query_df('select 1 as a, 2 as b', client)   # polars.DataFrame
arrow = bigquery101.get_bigquery_result('select 1 as a', client)  # pyarrow.Table

Context manager

CTX carries the client and closes it on exit:

with bigquery101.CTX('my-project') as bq:
    df = bq.query_df('select 1 as a')
    bq.dataframe_to_table(df, 'my_dataset', 'my_table')

Load a DataFrame into a table

bigquery101.dataframe_to_table(
    df,
    dataset_id='my_dataset',
    table_id='my_table',
    client=client,
    write_disposition='append',  # 'append' (default) | 'truncate' | 'empty'
    wait=True,                   # block until the load finishes
)

write_disposition controls existing data. 'append' (the default) adds rows, 'truncate' replaces all existing rows, and 'empty' fails if the table is not empty. Truncation is opt-in — the default never destroys data.

Pass wait=False to get the running job back without blocking.

Append one table onto another

bigquery101.append_table(
    source_table_ref='my-project.my_dataset.source',
    dest_dataset='my_dataset',
    dest_table='dest',
    client=client,
    safe=True,   # create the destination if it doesn't exist
)

Source and destination schemas must be compatible (insert ... select *).

Testing without BigQuery

The query/load helpers are typed against the BigQueryClient Protocol rather than the concrete google.cloud.bigquery.Client. A real client satisfies the protocol structurally, so you can pass a lightweight fake in tests — no mocking required. See tests/test_unit.py for the pattern.

uv run pytest -m "not integration"   # fast, hermetic unit tests
uv run pytest -m integration         # hits real BigQuery; needs GCP_PROJECT

Integration tests are skipped unless GCP_PROJECT is set.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bigquery101-0.2.0.tar.gz (46.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bigquery101-0.2.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file bigquery101-0.2.0.tar.gz.

File metadata

  • Download URL: bigquery101-0.2.0.tar.gz
  • Upload date:
  • Size: 46.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for bigquery101-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0a01aa99153da0ed889d6b2671f0bc3b734870c67f043249176e1ecf842273df
MD5 10145263fd56615ef6d24bb9f6c093cb
BLAKE2b-256 8a743327f25e4b162415b3ed2eca6ca4388d3cae9f9633ee96914ea26d964502

See more details on using hashes here.

File details

Details for the file bigquery101-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: bigquery101-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for bigquery101-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0cdba8845c03c47c08e314275d83205d72b5e72d450ee2eedf7ac6440f5a0b1e
MD5 3b7f05746c1f6001997580bd60e0807c
BLAKE2b-256 2d305f27f85ef66008eda4566ebe35652d49a71341f8423d78762f445e419554

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page