Skip to main content

A BigQuery connector for easy data syncing for your custom import. Checks the last date in the table and only appends new data or creates a new table. Made for Google Cloud Functions.

Project description

simple-bigquery-gcf-sync

Created so you only have to focus on the logic for your custom connector. The package takes care of:

  • Creating a table if it does not exist
  • Getting the latest date in the table if it exists, setting start date to the following day until yesterday
  • Conditionally running locally or using a Cloud Function service account

Note:

  • Currently defaults to montly table partitioning
  • For running locally your project should contain service account key.json in the same folder
  • You must provide a default_start_date and source_schema

Example usage

# Settings
source_schema = [
    bigquery.SchemaField("date", "DATE"),
    bigquery.SchemaField("order_id", "STRING"),
    bigquery.SchemaField("order_amount", "FLOAT"),
]
default_start_date = "2022-01-01"  # Required

def get_data_from_source(start_date, end_date):
    # Your custom source logic, should use start and end date


def main(request):
    print("⏩ Cloud Function invoked")
    project_id = request.args.get("project_id")
    dataset_id = request.args.get("dataset_id")
    table_id = request.args.get("table_id")

    BigQueryConnector.initialize(project_id, dataset_id, table_id)
    BigQueryConnector.ensure_table_exists(source_schema)
    start_date, end_date = BigQueryConnector.get_date_range(default_start_date)
    data = get_data_from_source(start_date, end_date, pub_key, priv_key)
    BigQueryConnector.load_data(data)

    print("✅ Data processing completed successfully")
    return


### For local testing, do not copy into cloud function
if __name__ == "__main__":

    class MockRequest:
        args = {
            "project_id": "project-name",
            "dataset_id": "dataset-name",
            "table_id": "table-name",
        }

    main(MockRequest())

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

simple-bigquery-gcf-sync-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

simple_bigquery_gcf_sync-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file simple-bigquery-gcf-sync-0.1.0.tar.gz.

File metadata

File hashes

Hashes for simple-bigquery-gcf-sync-0.1.0.tar.gz
Algorithm Hash digest
SHA256 566b196af1bae31e1e19e52c12ff2ff3aa6ad44d2c46aaa5252ecda21c991c78
MD5 a2f5ae5db3d837e79fdb0940042d057c
BLAKE2b-256 83249da05f7a603ea05211d6f0e4cf469be454dac672f694280e150838ea6166

See more details on using hashes here.

File details

Details for the file simple_bigquery_gcf_sync-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_bigquery_gcf_sync-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71e1008b2af1b0dc1afc0f6ee2bf40a42e1c89da20aa242337175d60309d0d34
MD5 4b25cf11e07710438ee5ab812d3db359
BLAKE2b-256 3dd352b9f2e62c8d6b62a05db81d75b3014d9e42237613be599fc337cc8870e2

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