Skip to main content

Easily integrate data in BigQuery

Project description

PyGBQ

Easily integrate data in BigQuery

Example

from pygbq import Client
import requests

client = Client()
token = client.get_secret('secret_name')
headers = {'Authorization': f'Bearer {token}'}
url = ...
data = requests.get(url, headers=headers).json()
response = client.update_table_using_temp(data=data, table_id='mydataset.mytable', how=['id'])

This snippets gets some data from an url and (merges)[https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#merge_statement] ((upserts)[https://en.wikipedia.org/wiki/Merge_(SQL)]) on id column it to the table mytable in the dataset mydataset.

Install and set up

pip install pygbq

Set up the authentication.

How it works

how=['column1', 'column2', ...]

PyGBQ generates one or many temporary tables that are merged into the target table. During the merge all the columns of the target table are updated. Here's how it looks like:

  1. Split data into batches.
  2. For every batch create mydataset.mytable_tmp_SOMERANDOMPOSTFIX, put it inside and run
MERGE myproject.mydataset.mytable T
USING myproject.mydataset.mytable_tmp_SOMERANDOMPOSTFIX S
ON T.column1 = S.column1 AND T.column2 = S.column2
WHEN NOT MATCHED THEN
	INSERT ROW
WHEN MATCHED THEN
	UPDATE SET
column1 = S.column1,
column2 = S.column2,
column3 = S.column3,
column4 = S.column4
...

how='replace'

  1. Creates a table mydataset.mytable with schema automatically generated by bigquery-schema-generator.
  2. Splits data into batches and inserts it to mydataset.mytable.

how='fail'

Identical to how='replace' except that it fails if mydataset.mytable exists.

how='insert'

Splits data into batches and inserts (appends) it to mydataset.mytable.

For more details look at Documentation section.

Documentation

Here's the documentation with default parameters.

Client

init

from pygbq import Client
client = Client(default_dataset=None, path_to_key=None)

Initalizes a client. You can specify:

  • default_dataset - (str) default dataset that the client will be using to reference tables
  • path_to_key - (str) By default PyGQB uses from google.auth import default to get credentials, but you can specify this parameter if you wish to use from google.auth import load_credentials_from_file instead.

update_table_using_temp

client.update_table_using_temp(data, table_id, how, schema: Union[str, List[dict]] = None, expiration=1, max_insert_num_rows=4000)

Updates table.

  • data - list of dict
  • table_id - (str) Table id, could have one of the following forms:
    • table_name if default_dataset is set
    • dataset_name.table_name
    • project_id.dataset_name.table_name
  • how - (str or List[dict]) Look at How it works section
  • expiration - (float) temporary tables expiration time in hours
  • max_insert_num_rows - (int) how many rows per temporary table is inserted

get_secret

client.get_secret(self, secret_id, version="latest")

Get a secret stored in Secret Manager.

  • secret_id - (str) Secret name
  • version - Secret version

add_secret

client.get_secret(self, secret_id, version="latest")

Adds a new secret version in Secret Manager.

  • secret_id - (str) Secret name
  • data - (str) Secret value

read_jsonl

from pygbq import read_jsonl
read_jsonl(name: str = "data.jsonl")

Reads a new line delimited json.

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

pygbq-0.25.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

pygbq-0.25-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file pygbq-0.25.tar.gz.

File metadata

  • Download URL: pygbq-0.25.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5

File hashes

Hashes for pygbq-0.25.tar.gz
Algorithm Hash digest
SHA256 30bfcd1eeffcccffb994bf56a6b787099a50e0f2f68b3c6564f96fc529f33a5d
MD5 e2eed72e4d7e03df09890a01cf64466d
BLAKE2b-256 d3c7d9e0fb6139e74c6ac8140a15ec1f70e35a7612611f0e56fd39d79ad37abf

See more details on using hashes here.

File details

Details for the file pygbq-0.25-py3-none-any.whl.

File metadata

  • Download URL: pygbq-0.25-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5

File hashes

Hashes for pygbq-0.25-py3-none-any.whl
Algorithm Hash digest
SHA256 c404d52b51fe782fe829d09f5330ac1517eef086f519469c784902275f1e3881
MD5 51f80b2bc37ee9ecae4e03496d05ce1d
BLAKE2b-256 1cd1481018cff34cee9e83b137bcc90169c288b63f009b85f5f64f1b5e8b049f

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