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(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:
- Split
datainto batches. - 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'
- Creates a table
mydataset.mytablewith schema automatically generated bybigquery-schema-generator. - 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 theclientwill be using to reference tablespath_to_key- (str) By default PyGQB usesfrom google.auth import defaultto get credentials, but you can specify this parameter if you wish to usefrom google.auth import load_credentials_from_fileinstead.
update
client.update(data, table_id, how, schema: Union[str, List[dict]] = None, expiration=1, max_insert_num_rows=4000)
Updates table.
data- list of dicttable_id- (str) Table id, could have one of the following forms:table_nameifdefault_datasetis setdataset_name.table_nameproject_id.dataset_name.table_name
how- (str or List[dict]) Look at How it works sectionexpiration- (float) temporary tables expiration time in hoursmax_insert_num_rows- (int) how many rows per temporary table is inserted
query
client.query(query)
Execute a query in BigQuery.
query- (str) BigQuery query
get_secret
client.get_secret(self, secret_id, version="latest")
Get a secret stored in Secret Manager.
secret_id- (str) Secret nameversion- Secret version
add_secret
client.get_secret(self, secret_id, version="latest")
Adds a new secret version in Secret Manager.
secret_id- (str) Secret namedata- (str) Secret value
read_jsonl
from pygbq import read_jsonl
read_jsonl(name: str = "data.jsonl")
Reads a new line delimited json.
Metadata
Release files for pygbq 0.26
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygbq-0.26.tar.gz | 11.8 kB | Without provenance | Details for pygbq-0.26.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygbq-0.26-py3-none-any.whl | Python 3 | none | any | Details for pygbq-0.26-py3-none-any.whl |
Total release size: 23.5 kB
Release files / pygbq-0.26.tar.gz
| Download URL | pygbq-0.26.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
39acb52e1f2341ceca2cd3736fed834dd12d2b91c9fb4cc7f1beba4660067b88
|
|
BLAKE2b-256 checksum How to use checksums |
3f9802c1bdde58c7fd8102acfd468b27f56986f23bc9388e5f0d8bf9ee6b8a78
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / pygbq-0.26-py3-none-any.whl
| Download URL | pygbq-0.26-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
466452ba80a55ec053d4bc3d546b2c1da13b86c89268cc6b3e422d0c4d989eb2
|
|
BLAKE2b-256 checksum How to use checksums |
4ab08337002fdb312959cfdf44fada11ed32494f6bde1ef882de985273970082
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for pygbq 0.26
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.