Skip to main content

GCP Utility tool

We can backfill any kind of bigquery sql to specific destination table with powerful python jinja templating feature. By default it can support date templates in your query. It will automatically convert to date template to date value in your query while executing.

Following are the available date templates you can use it in your query.

{{ds}} - YYYY-DD-MM date string 
{{ds_nodash}} - YYYYDDMM date string 
{{ds_yesterday}} - YYYY-DD-MM yesterday date string 
{{ds_yesterday_nodash}} - YYYYDDMM yesterday date string 
{{ds_tomorrow}} - YYYY-DD-MM tomorrow date string 
{{ds_tomorrow_nodash}} - YYYYDDMM tomorrow date string 

Getting Started

Prerequisites

What things you need to install the software and how to install them

python 2.7
google-cloud==0.32.0
Jinja2==2.10

Installing

Use virtualenv to Install the dependancy packages

git clone https://gitlab.com/manojnuk50/GCPUtility
cd GCPUtility
virtualenv venv
activate venv/bin/activate
pip install -e .

-h command will return the help content which contains all the available options

bq_client -h

End with an example of getting some data out of the system or using it for a little demo

Available option

usage: bq_client.py [-h] [-q QUERY] [-f] [-p PROJECT] [-ssql] [-dml]
                    [-d DESTINATION_TABLE] [-w WRITE_DESPOSITION]
                    [-qf QUERY_FILE] [-t TEMPLATE] [-tf TEMPLATE_FILE]
                    [-s START_DATE] [-e END_DATE] [-sf SERVICE_ACCOUNT_PATH]

optional arguments:
  -h, --help            show this help message and exit
  -q QUERY, --query QUERY
                        provide valid bigquery sql
  -f, --no-flattern     Flattern results
  -p PROJECT, --project_id PROJECT
                        provide valid project id
  -ssql, --standard-sql
                        Mention if using Standard sql
  -dml, --dml-statement
                        Mention if using DML statements in your query
  -d DESTINATION_TABLE, --destination-table DESTINATION_TABLE
                        <projectname>:<datasetid>.<tableid> provide valid
                        destination project-id
  -w WRITE_DESPOSITION, --write-desposition WRITE_DESPOSITION
                        Write disposition value
  -qf QUERY_FILE, --query-file QUERY_FILE
                        provide bigquery sql filepath
  -t TEMPLATE, --template TEMPLATE
                        provide template values
  -tf TEMPLATE_FILE, --template-file TEMPLATE_FILE
                        provide template file path
  -s START_DATE, --start-date START_DATE
                        Provide valid startdate (YYYY-MM-DD)
  -e END_DATE, --end-date END_DATE
                        Provide valid end date (YYYY-MM-DD)
  -sf SERVICE_ACCOUNT_PATH, --service-account-file-path SERVICE_ACCOUNT_PATH
                        provide valid path of service account json file```

passing raw query

you can pass the raw query to script that need to be execute

bq_client -q "select * from [sample_proj:datasetid.tableid]" -p destination_projectid -d dest_datasetid.tableid

Using sql file

if your query is quite long, you can save it to text file and pass the path of it

bq_client --query-file "/path/sample.sql" -p destination_projectid -d dest_datasetid.tableid```

Passing date parameter

In default it will substitute current date into template fields. but if you want to change the date value you can pass manually using start date parameter

bq_client --query-file sample.sql -p projectid -d dest_datasetid.tableid -s 2018-02-02

Backfill

Pass start-date and end-date to script, it will backfill the data.

bq_client --query-file sample.sql -p projectid -d dest_datasetid.tableid -s 2018-02-02 -e 2018-02-10

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

gcp_utility-1.1.6-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file gcp_utility-1.1.6-py2.py3-none-any.whl.

File metadata

  • Download URL: gcp_utility-1.1.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/2.7.16

File hashes

Hashes for gcp_utility-1.1.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3ffb44ecf2104b9b5d62e4500641d9f210b28eebc7d3d10066f1d08f10b0fa6d
MD5 2adcd7884052a42c642d36ded48a7cf4
BLAKE2b-256 c2873dad2908cc01f44234c5b0e284dcc39e5d6da865da5f6acafc2159ac3101

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