Skip to content

Instantly share code, notes, and snippets.

@tanan
Created December 4, 2018 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanan/ab9b5fb539d3a5e9941f8add6116dd23 to your computer and use it in GitHub Desktop.
Save tanan/ab9b5fb539d3a5e9941f8add6116dd23 to your computer and use it in GitHub Desktop.
from google.cloud import bigquery
client = bigquery.Client()
def bq_import(event, context):
"""Triggered by a change to a Cloud Storage bucket.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
file = event
uri = 'gs://{0}/{1}'.format(file['bucket'],file['name'])
table_ref = client.dataset('foo').table('bar')
job_config = bigquery.LoadJobConfig()
job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND
job_config.autodetect = True
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
load_job = client.load_table_from_uri(
uri,
table_ref,
job_config=job_config)
assert load_job.job_type == 'load'
load_job.result()
assert load_job.state == 'DONE'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment