【问题标题】:Bigquery api using python ApiBigquery api 使用 python Api
【发布时间】:2021-08-20 12:40:04
【问题描述】:

我正在尝试通过读取存储桶原始文件(基于它应该创建相同表的存储桶文件名)来自动在 BQ 中创建表,这将使用 yml 作为配置。 任何人都可以提供有关如何使用代码示例编写的线索。

【问题讨论】:

    标签: python google-bigquery yaml pyyaml


    【解决方案1】:

    我正在做类似的事情。这还取决于您希望如何读取存储桶“原始文件”,在我的情况下是 GCS 通知 + PubSub。

    非常简单的例子:

    uri = "gs://" + event['attributes']['bucketId'] + "/" + filename
    table = os.path.splitext(filename)[0]
    
    #Format the filename to only numbers and letters (no special characters)
    table = re.sub('[^A-Za-z0-9]+', '', table)
    
    # Construct a BigQuery client object.
    client = bigquery.Client()
    
    # Name of the table which will be created automatically by BigQuery
    table_id = project_id + "." + dataset_id + "." + table
    
    job_config = bigquery.LoadJobConfig(
        autodetect=True,
        source_format=bigquery.SourceFormat.CSV,
    )
    
    load_job = client.load_table_from_uri(
            uri, table_id, job_config=job_config
    )
    

    如果表不存在,BigQuery 作业会自动创建。

    【讨论】:

    • 感谢您的输入,并想了解您是如何读取表格和其他详细信息(如数据集、存储桶等)是否来自配置文件?如果可以,请您详细说明一下
    猜你喜欢
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 2022-12-18
    相关资源
    最近更新 更多