PyBigQuery (OBSOLETE)

0.10.2 · abandoned · verified Fri Apr 17

PyBigQuery is an **obsolete** SQLAlchemy dialect for Google BigQuery. It is no longer actively maintained and should not be used for new projects. The last version released was 0.10.2, in 2019. Users are strongly advised to migrate to `google-cloud-bigquery-sqlalchemy` (PyPI package `google-cloud-bigquery-sqlalchemy`, GitHub repository `googleapis/python-bigquery-sqlalchemy`) which is the actively maintained and modern successor.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates establishing a connection to Google BigQuery using the obsolete PyBigQuery dialect with SQLAlchemy. It relies on standard Google Cloud authentication. This code is provided for historical reference; users should migrate to `google-cloud-bigquery-sqlalchemy` for any new development or maintenance.

import os
from sqlalchemy import create_engine, text

# Ensure GOOGLE_APPLICATION_CREDENTIALS points to your service account key file
# or use `gcloud auth application-default login` for Application Default Credentials.
# Replace 'your-gcp-project-id' and 'your_dataset' with actual values or set env vars.
project_id = os.environ.get('BIGQUERY_PROJECT_ID', 'your-gcp-project-id')
dataset_id = os.environ.get('BIGQUERY_DATASET_ID', 'your_dataset')

if project_id == 'your-gcp-project-id':
    print("WARNING: Set BIGQUERY_PROJECT_ID env var or replace 'your-gcp-project-id'.")
if dataset_id == 'your_dataset':
    print("WARNING: Set BIGQUERY_DATASET_ID env var or replace 'your_dataset'.")

try:
    # Connect using the 'bigquery' dialect provided by pybigquery
    # Note: For production, ensure credentials are set securely.
    engine = create_engine(f"bigquery://{project_id}/{dataset_id}")

    with engine.connect() as connection:
        # Example query (replace with your specific table/data)
        # Note: 'text()' is required for string-based SQL statements in SQLAlchemy 1.4+/2.0.
        result = connection.execute(text("SELECT 1 as num, 'hello' as str"))
        for row in result:
            print(f"Retrieved row: num={row.num}, str='{row.str}'")

    print("Successfully connected to BigQuery using PyBigQuery (obsolete).")
except Exception as e:
    print(f"An error occurred: {e}")
    print("Troubleshooting hint: Check BigQuery project/dataset IDs, credentials, and Python version.")
    print("RECOMMENDATION: Migrate to 'google-cloud-bigquery-sqlalchemy' for active support.")

view raw JSON →