villebro / pybigquery

SQLAlchemy dialect for BigQuery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SQLAlchemy dialect and API client for BigQuery.

Usage

SQLAchemy

API Client

Project

project in bigquery://project is used to instantiate BigQuery client with the specific project ID. To infer project from the environment, use bigquery:// – without project

Authentication

Follow the Google Cloud library guide for authentication. Alternatively, you can provide the path to a service account JSON file in create_engine():

Location

To specify location of your datasets pass location to create_engine():

Table names

To query tables from non-default projects, use the following format for the table name: project.dataset.table, e.g.:

Batch size

By default, arraysize is set to 5000. arraysize is used to set the batch size for fetching results. To change it, pass arraysize to create_engine():

Adding a Default Dataset

If you want to have the Client use a default dataset, specify it as the "database" portion of the connection string.

When using a default dataset, don't include the dataset name in the table name, e.g.:

Note that specyfing a default dataset doesn't restrict execution of queries to that particular dataset when using raw queries, e.g.:

Connection String Parameters

There are many situations where you can't call create_engine directly, such as when using tools like Flask SQLAlchemy. For situations like these, or for situations where you want the Client to have a default_query_job_config, you can pass many arguments in the query of the connection string.

The credentials_path, location, and arraysize parameters are used by this library, and the rest are used to create a QueryJobConfig

Note that if you want to use query strings, it will be more reliable if you use three slashes, so 'bigquery:///?a=b' will work reliably, but 'bigquery://?a=b' might be interpreted as having a "database" of ?a=b, depending on the system being used to parse the connection string.

Here are examples of all the supported arguments. Any not present are either for legacy sql (which isn't supported by this library), or are too complex and are not implemented.

Creating tables

To add metadata to a table:

To add metadata to a column:

Requirements

Install using

  • pip install pybigquery

Testing

Load sample tables:

./scripts/load_test_data.sh

This will create a dataset test_pybigquery with tables named sample_one_row and sample.

Set up an environment and run tests:

pyvenv .env
source .env/bin/activate
pip install -r dev_requirements.txt
pytest

About

SQLAlchemy dialect for BigQuery

License:MIT License


Languages

Language:Python 97.5%Language:Shell 2.5%