python-3.x - 在 Bigquery 中为多个 CSV 文件自动创建表
问题描述
每当使用 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 对象中的数据加载到表中。
推荐阅读
- firebase - 类型“_AssertionError”不是“字符串”类型的子类型
- excel - vba 如果满足单元格中的条件
- reactjs - 输入字段中的值不显示
- c++ - 有什么方法可以将数据从一个类继承到另一个类?
- r - 解析表在 pdf 上的多个页面中,在单个表中,在 r 中
- laravel - 如何通过读取`Laravel`中的JSON文件动态填充和编辑`Nova`字段
- validation - 一个域可以只包含 TLD(顶级域)吗?
- swift - 将值设置为不断变化的默认值
- wordpress - Wordpress 允许页面永久链接作为日期(年份)而不在末尾附加 -2
- python - 连接和排序表无法按预期工作