首页 > 解决方案 > BigQuery 表格标签在更新数据后消失

问题描述

我已经使用网络用户界面为 BigQuery 表设置了标签。但是,当我使用 Python 或 Nodejs 客户端库(参见下面的代码示例)将新数据附加到表中时,标签会消失。这是预期的行为吗?如果是这样,有没有办法在不丢失标签的情况下更新表数据?节点:

const metadata = {
      sourceFormat: "NEWLINE_DELIMITED_JSON",
      autodetect: true,
      writeDisposition: "WRITE_APPEND",
};
const [job] = await bigquery
      .dataset(datasetId)
      .table(tableId)
      .load(filename, metadata);

Python:

data.to_gbq(
            'datasetname.tablename', 
            project_id='projectid', 
            if_exists='replace'
        )

标签: google-bigquery

解决方案


我尝试使用下面的代码重现您的问题,正如您在图像中看到的那样,标签没有受到影响。

from google.cloud import bigquery

client = bigquery.Client("my-project-id")

filename = "/path/to/file"
dataset_ref = client.dataset("dataset_id")
table_ref = dataset_ref.table("table_id")

job_config = bigquery.LoadJobConfig()
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON

with open(filename, "rb") as source_file:
     job = client.load_table_from_file(source_file, table_ref, job_config=job_config)



  1. 通过接口创建的带有标签的表 在此处输入图像描述
  2. 运行代码 在此处输入图像描述
  3. 标签未修改 在此处输入图像描述



编辑

当使用 Pandas 写入 BigQuery 时,如果您将属性“if_exists”设置为“replace”,则该表将被删除并创建另一个表,可以在此处找到。

在此处输入图像描述


推荐阅读