Skip to main content

Python Tools for BigQuery

Project description

Build Status PyPI version

Python Tools for BigQuery

Why?

For data collection and data exploration, we like to work with BigQuery. But we have not found a python library, to easily handle recurring tasks like adding new data (of potentially inconsistent schema) and schema migrations. So we took a couple of our solutions for those tasks and put them into this library.

What?

bqtools provides a light-weight solution to explicit schema management with python-native types (unlike pandas dtype) and some convenient type checking, inference and conversions. Table-objects created by bqtools can be read from BigQuery, stored locally, read from a local file and written to BigQuery. Table schemas can be changed and data can be added or modified.

Install

pip install --upgrade bqtools

Examples:

Create basic tables

from fourtytwo import bqtools

schema = [
    {'name': 'number', 'field_type': 'INTEGER'},
    {'name': 'text', 'field_type': 'STRING'},
    {'name': 'struct', 'field_type':'RECORD', 'mode':'REPEATED', 
        'fields': [
            {'name':'integer', 'field_type':'INTEGER'},
            {'name':'text', 'field_type':'STRING'}
        ]
    }
]
# valid BigQuery types see: 
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types
# geo and array are currently not/not fully supported

# data = columns of lists
table = bqtools.BQTable(
    schema=schema, 
    data=[[1, 2, 3, 4], ['a', 'b', 'c', 'd']]
)

# data = rows of dicts
table = bqtools.BQTable(
    schema=schema, 
    data=[
        {'number': 1, 'text': 'a'}, 
        {'number': 2, 'text': 'b'},
        ...
    ]
)

View data

print(table.data)       # list of all columns
print(table.rows(n=10)) # list of first n rows

# convert to pandas.DataFrame
df = table.to_df()               
# warning: pandas dtypes may be inconsistent 
# with BigQuery Schema field_types

Append data

rows = [{'number': 5, 'text': 'e'}]
table.append(rows)

row = [[6, 'f']]
table.append(rows)

Load table from BigQuery

# requires environment variable GOOGLE_APPLICATION_CREDENTIALS 
# or parameter credentials='path-to-credentials.json'
table = bqtools.read_bq(
    table_ref='project_id.dataset_id.new_table_id', 
    limit=10,           # limit query rows
    schema_only=False   # set True to only add data
)

Modify table schema

# change column order and field_type
new_schema = [
    {'name': 'text', 'field_type': 'STRING'},
    {'name': 'number', 'field_type': 'FLOAT'},
]
table.schema(new_schema)

# change column names
table.rename(columns={'number': 'decimal'})

Write table to BigQuery

# requires environment variable GOOGLE_APPLICATION_CREDENTIALS
# or parameter credentials='path-to-credentials.json'
table.to_bq(table_ref, mode='append')

Persist tables locally

# write to local file (compressed binary format)
table.save('local_table.bqt')

# load from local file
table = bqtools.load('local_table.bqt')

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

bqtools-0.6.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distributions

bqtools-0.6.0-py3.7.egg (17.9 kB view details)

Uploaded Source

bqtools-0.6.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file bqtools-0.6.0.tar.gz.

File metadata

  • Download URL: bqtools-0.6.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.8

File hashes

Hashes for bqtools-0.6.0.tar.gz
Algorithm Hash digest
SHA256 8af25c798fa2c6a8338336c3d7f37bd38035fe5aa8bc61671a3c6afb57c84c6f
MD5 2d75793d1ee679b8a3646fbd2de80153
BLAKE2b-256 785653d2e7beca6d6223e5fd52dcdd095341b66ca6bb1ebe944d6502baf27c21

See more details on using hashes here.

File details

Details for the file bqtools-0.6.0-py3.7.egg.

File metadata

  • Download URL: bqtools-0.6.0-py3.7.egg
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.8

File hashes

Hashes for bqtools-0.6.0-py3.7.egg
Algorithm Hash digest
SHA256 5fa43bc8c48fb6440e05219fc8b83853dab6ab30e2be94c43984b2ae6b3e0ce2
MD5 212f7c2afb1ce5fdcff27c363587b44f
BLAKE2b-256 d12a76eb23c64a4ceb94ccb4037631a2e2b92bdff6043d4ba2bb308ba5a73ba8

See more details on using hashes here.

File details

Details for the file bqtools-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: bqtools-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.8

File hashes

Hashes for bqtools-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2fa877a0eb58b89d7eb75be5fc4ae27b206b2cded4421d6099f330917dcf34ce
MD5 bba4581de882df5bfaa797a35dfdead0
BLAKE2b-256 9cd67cb4fa73cfbae8bb0266941cd11d88857511215a46e691bb6af21aa3071e

See more details on using hashes here.

Supported by

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