【问题标题】:Appending CSV to BigQuery table with Python client使用 Python 客户端将 CSV 附加到 BigQuery 表
【发布时间】:2020-01-10 17:03:56
【问题描述】:

我每周都有一个格式相同的新 CSV 文件,我需要使用 Python 客户端将其附加到 BigQuery 表中。我使用第一个 CSV 成功创建了表,但我不确定如何附加后续的 CSV。我发现的唯一方法是 google.cloud.bigquery.client.Client().insert_rows() 方法。见 api 链接here。这需要我首先将 CSV 作为字典列表读取。有没有更好的方法将数据从 CSV 追加到 BigQuery 表?

【问题讨论】:

    标签: python python-3.x google-bigquery python-bigquery


    【解决方案1】:

    看下面的简单例子

    # from google.cloud import bigquery
    # client = bigquery.Client()
    # table_ref = client.dataset('my_dataset').table('existing_table')
    
    job_config = bigquery.LoadJobConfig()
    job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND
    job_config.skip_leading_rows = 1
    
    # The source format defaults to CSV, so the line below is optional.
    job_config.source_format = bigquery.SourceFormat.CSV
    uri = "gs://your_bucket/path/your_file.csv"
    load_job = client.load_table_from_uri(
        uri, table_ref, job_config=job_config
    )  # API request
    print("Starting job {}".format(load_job.job_id))
    
    load_job.result()  # Waits for table load to complete.
    print("Job finished.")
    
    destination_table = client.get_table(table_ref)
    print("Loaded {} rows.".format(destination_table.num_rows))  
    

    BigQuery Documentation查看更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多