Skip to content

Instantly share code, notes, and snippets.

@danthelion
Created May 26, 2021 13:27
Show Gist options
  • Save danthelion/1b83e22f144c8471cf7bc573399fc254 to your computer and use it in GitHub Desktop.
Save danthelion/1b83e22f144c8471cf7bc573399fc254 to your computer and use it in GitHub Desktop.
def main(data, context, dataset='test_dataset_dev', table='beer'):
print('Event ID: {}'.format(context.event_id))
print('Event type: {}'.format(context.event_type))
print('Importing required modules.')
from google.cloud import bigquery
print('This is the data: {}'.format(data))
input_bucket_name = data['bucket']
source_file = data['name']
uri = 'gs://{}/{}'.format(input_bucket_name, source_file)
print('Getting the data from bucket "{}"'.format(uri))
client = bigquery.Client()
dataset_ref = client.dataset(dataset)
job_config = bigquery.LoadJobConfig()
job_config.autodetect = True
job_config.schema_update_options = [bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION]
job_config.source_format = bigquery.SourceFormat.CSV
job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND
load_job = client.load_table_from_uri(uri, dataset_ref.table(table), job_config=job_config)
print('Starting job {}'.format(load_job.job_id))
load_job.result()
print('Job finished.')
destination_table = client.get_table(dataset_ref.table(table))
print('Loaded {} rows.'.format(destination_table.num_rows))
print('File imported successfully.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment