【问题标题】:Replace BigQuery table with API job用 API 作业替换 BigQuery 表
【发布时间】:2019-04-01 11:49:07
【问题描述】:

我正在使用BigQuery client libraries 执行数据 ETL jpb,然后将数据加载回 BigQuery。

我想每次都覆盖目标表,但目前我的代码每次运行时都会将新数据附加到表中。我已阅读有关 job_config 的文档,并使用它来设置查询参数,但我不知道如何为查询设置写入处置。

这是我迄今为止尝试过的:

roc_df = pd.DataFrame(roc_score)

job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE

dataset_ref = client.dataset('Customers')
table_ref = dataset_ref.table('propensity_scores_test')

client.load_table_from_dataframe(roc_df, table_ref, job_config=job_config).result()

我也试过这种格式:

query_config = bigquery.QueryJobConfig(
    query_parameters=[
        bigquery.job.WriteDisposition('WRITE_TRUNCATE')
    ]
)

但两者当前都返回错误:

错误请求:400 POST https://www.googleapis.com/upload/bigquery/v2/projects/my_project/jobs?uploadType=resumable: 缺少必需的参数

我可以写出我的数据并每次都替换表格吗?

【问题讨论】:

  • 您是在尝试从查询结果或加载作业中写入/截断表吗?
  • 我认为这个question 可能会对你有所帮助
  • @GrahamPolley 我不确定!我正在使用 train = client.query(training_query).to_dataframe() 加载数据而没有任何配置集 - 我只需要稍后使用配置来设置写入规则,也许这会让人感到困惑?

标签: google-bigquery


【解决方案1】:

load_table_from_dataframe 方法使用LoadJobConfig。这是一个有效的sn-p代码:

from google.cloud import bigquery
import pandas as pd

roc_df = pd.DataFrame([{"firstName": "Foo", "lastName": "Bar"}])

client = bigquery.Client()

dataset_ref = client.dataset('my_dataset')
table_ref = dataset_ref.table('my_table')

job_config = bigquery.job.LoadJobConfig()
job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE

client.load_table_from_dataframe(roc_df, table_ref, job_config=job_config)

您的代码的唯一更改是:

job_config = bigquery.job.LoadJobConfig()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多