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

标签 python-3.x google-bigquery google-cloud-functions

每当使用python中的云功能将文件上传到存储桶时,我想在Bigquery中自动生成表。
例如 - 如果将 sample1.csv 文件上传到存储桶,则将在 Bigquery 中创建一个 sample1 表。
如何使用 Python 使用云函数自动执行我尝试使用以下代码但能够生成 1 个表并且所有数据都附加到该表中,如何进行

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 文件/对象的名称以触发您的函数。
  • 更新 table_id在您的示例代码中,根据您在第一步中提取的文件名设置表名。
  • 更新 uri在您的示例代码中仅使用单个文件作为输入。如所写,您的示例尝试将数据从 GCS 中所有匹配的 CSV 对象加载到表中。
  • 关于python-3.x - 在 Bigquery 中为多个 CSV 文件自动创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67602231/

    相关文章:

    javascript - 每个其他请求的状态 500 "Error: Could not handle the request"

    python - 通过Python访问Indeed

    python - 为什么我的 lambda 不起作用?

    regex - 使用正则表达式从 Google BigQuery 中的 url 中提取完整域

    javascript - 当按钮点击错误时发送通知

    node.js - 如何使用 Firebase Cloud Functions .npmrc 设置私有(private) NPM 模块?

    python - 我希望能够从根路径和子路径加载类

    linux - 安装的 tesserocr(python) 看不到 leptonica

    google-analytics - GA bigquery 表中 hits.page.pagePath 的 session

    google-bigquery - 有没有办法在 bigquery 之外合并 bigquery hyperloglog 草图?