Python 3.x 在Bigquery中为多个CSV文件自动创建表

Python 3.x 在Bigquery中为多个CSV文件自动创建表,python-3.x,google-bigquery,google-cloud-functions,Python 3.x,Google Bigquery,Google Cloud Functions,我想在python中使用云函数在存储桶中上传文件时,在Bigquery中自动生成表 例如,如果sample1.csv文件上载到bucket,则将在Bigquery中创建一个sample1表。 如何使用Python使用云函数实现自动化我尝试了以下代码,但能够生成一个表,所有数据都附加到该表中,如何继续 def hello_gcs(event, context): from google.cloud import bigquery # Construct a BigQuery cli

我想在python中使用云函数在存储桶中上传文件时,在Bigquery中自动生成表

例如,如果sample1.csv文件上载到bucket,则将在Bigquery中创建一个sample1表。 如何使用Python使用云函数实现自动化我尝试了以下代码,但能够生成一个表,所有数据都附加到该表中,如何继续

def hello_gcs(event, context):
    from google.cloud import bigquery
    # Construct a BigQuery client object.
    client = bigquery.Client()

    # TODO(developer): Set table_id to the ID of the table to create.
    table_id = "test_project.test_dataset.test_Table"

    job_config = bigquery.LoadJobConfig(
    autodetect=True,
    skip_leading_rows=1,
    # The source format defaults to CSV, so the line below is optional.
    source_format=bigquery.SourceFormat.CSV,
    )
    uri = "gs://test_bucket/*.csv"

    load_job = client.load_table_from_uri(
    uri, table_id, job_config=job_config
    )  # Make an API request.

    load_job.result()  # Waits for the job to complete.

    destination_table = client.get_table(table_id)  # Make an API request.
    print("Processing file: {file['name']}.")

听起来你需要做三件事:

  • 从接收到的通知事件中提取CSV文件/对象的名称以启动函数

  • 更新示例代码中的
    表\u id
    ,根据第一步提取的文件名设置表名

  • 更新示例代码中的
    uri
    ,使其仅使用单个文件作为输入。如前所述,您的示例尝试将数据从GCS中所有匹配的CSV对象加载到表中


  • 工作。。非常感谢。