【问题标题】:insert_rows_json with write truncateinsert_rows_json 与写截断
【发布时间】:2022-11-18 18:04:05
【问题描述】:

我正在编写一个小的 python 脚本,用于将从 API 获取的数据写入 BigQuery 表。我只有少量数据。来自 API 的数据每天更新。我希望能够每天用最新的数据覆盖表目标。

我在云函数中有以下脚本:

  data = {'col': 'value'} # the JSON response I get from the API (simplified output)

  job_config = bigquery.LoadJobConfig(
    schema = [
      bigquery.SchemaField("col", "STRING"),
    ],
    write_disposition="WRITE_TRUNCATE",
  )     

  job = bq_client.insert_rows_json(table_id, [data], job_config=job_config)

和以下错误Client.insert_rows_json() got an unexpected keyword argument 'job_config'

我应该使用不同于insert_rows_json()的方法吗?每天在同一张表中写入这些新数据的最佳方式是什么?

【问题讨论】:

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


    【解决方案1】:

    我认为这是最好的方法,但你不能通过 JobConfigWRITE_TRUNCATE 模式。您必须对 truncate 表执行单独的查询,然后将所有字典附加到 BigQueryinsert_rows_json

    data = {'col': 'value'} # the JSON response I get from the API (simplified output)
    
    # Truncate the table
    bq_client = bigquery.Client()
    query_job = bq_client.query(f"TRUNCATE table {table_id}")
    
    results = query_job.result()
    
    # Append your Dict from api to BQ
    job = bq_client.insert_rows_json(table_id, [data])
    

    insert_rows_json 使用以下 insertAll api

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多