Created
December 4, 2018 02:49
-
-
Save tanan/ab9b5fb539d3a5e9941f8add6116dd23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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