如何解决GCP Cloud函数可加载触发OBJECT_FINALIZE_EVENT_TYPE的文件
我正在使用以下Python GCP云功能将GCS存储桶中的csv文件加载到BigQuery表中。
def csv_in_gcs_to_table(bucket_name: str,object_name: str,dataset_id: str, table_id: str, schema: List[bigquery.SchemaField]) -> None:
"""Upload CSV to BigQuery table.
If the table already exists,it overwrites the table data.
Args:
bucket_name: Bucket name for holding the object
object_name: Name of object to be uploaded
dataset_id: Dataset id where the table is located.
table_id: String holding id of hte table.
schema: Schema of the table_id
"""
client = bigquery.Client()
dataset_ref = client.dataset(dataset_id)
job_config = bigquery.LoadJobConfig()
job_config.schema = schema
job_config.source_format = bigquery.SourceFormat.CSV
job_config.write_disposition = bigquery.WriteDisposition().WRITE_TRUNCATE
uri = "gs://{}/{}".format(bucket_name,object_name)
load_job = client.load_table_from_uri(uri, dataset_ref.table(table_id), job_config=job_config)
load_job.result()
每次在存储桶中放入新文件并选择与object_name参数对应的文件时都会触发该函数。
我希望加载功能选择上次上传到存储桶的文件,换句话说就是触发事件的文件。
我的问题是如何实现它。
解决方法
基于@FrankvanPuffelen的建议,我修改了该功能以捕获事件文件名。 传递包含所有事件变量的事件参数,包括触发事件的优良名称。
def csv_in_gcs_to_table(event,context):
from google.cloud import bigquery
client = bigquery.Client()
bucket_name = "bucket_name"
object_name = event['name']
table_id = "project_id.dataset_name.table_name"
schema = [
bigquery.SchemaField('col1','string'),bigquery.SchemaField('col2',]
job_config = bigquery.LoadJobConfig()
job_config.schema = schema
job_config.source_format = bigquery.SourceFormat.CSV
job_config.write_disposition = bigquery.WriteDisposition().WRITE_APPEND
job_config.skip_leading_rows = 1
uri = "gs://{}/{}".format(bucket_name,object_name)
load_job = client.load_table_from_uri(uri,table_id,job_config=job_config)
load_job.result()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。