【问题标题】:BigQuery: How to overwrite a table with bigquery.Client().copy_table methodBigQuery:如何使用 bigquery.Client().copy_table 方法覆盖表
【发布时间】:2018-10-12 08:25:31
【问题描述】:

这是我作为https://cloud.google.com/bigquery/docs/managing-tables#bigquery-copy-table-python 的参考的代码:

source_dataset = client.dataset('samples', project='bigquery-public-data')
source_table_ref = source_dataset.table('shakespeare')

# dataset_id = 'my_dataset'
dest_table_ref = client.dataset(dataset_id).table('destination_table')

job = client.copy_table(
    source_table_ref,
    dest_table_ref,
    # Location must match that of the source and destination tables.
    location='US')  # API request

job.result()  # Waits for job to complete.

在我的情况下,目标表存在,我收到此错误:

Already Exists

如何用这个 copy_table 方法覆盖?在 bq 命令行中,我可以使用 -f 选项。所以我正在寻找相同的标志。

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    您需要像这样将作业配置传递给请求:

    job_config = bigquery.CopyJobConfig()
    job_config.write_disposition = "WRITE_TRUNCATE"
    job = client.copy_table(
        source_table_ref,
        dest_table_ref,
        location='US',
        job_config=job_config)  # API request
    

    更多文档在这里:https://googleapis.github.io/google-cloud-python/latest/bigquery/generated/google.cloud.bigquery.client.Client.copy_table.html

    【讨论】:

    • 你拯救了我的一天,兄弟。
    • 一些示例代码的王国,你来了,我的救星!
    【解决方案2】:

    查看链接的文档:

    configuration.copy.writeDisposition:指定目标表已经存在时发生的动作。

    支持以下值:

    • WRITE_TRUNCATE:如果表已存在,BigQuery 会覆盖表数据。
    • WRITE_APPEND:如果表已存在,BigQuery 会将数据附加到表中。
    • WRITE_EMPTY:如果表已存在且包含数据,则作业结果中会返回“重复”错误

    默认值为 WRITE_EMPTY。

    【讨论】:

    • 我想你可能忘记链接文档了
    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多