BigQuery:表 ID 无效

Ric*_*son 1 google-bigquery

我正在尝试使用此示例代码插入一行:

https://developers.google.com/bigquery/streaming-data-into-bigquery#streaminginsertexamples

我收到一个错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid table ID \"projectid:datasetid.tableid\".",
    "reason" : "invalid"
  } ],
  "message" : "Invalid table ID \"projectid:datasetid.tableid\"."
}
Run Code Online (Sandbox Code Playgroud)

项目、数据集和表都存在。当我从错误报告中复制实际的 projectid:datasetid.tableid 并将其粘贴到 BigQuery Web UI 时,它可以工作。projectid 中有一个“-”,其余的是字母数字。为什么 API 会抱怨 Web UI 接受的 ID?


更新以回答乔丹的问题。这是我的电话:

TableDataInsertAllResponse response =
    bigquery
    .tabledata()
    .insertAll(projectId, datasetId, table.getId(), content)
    .execute();
Run Code Online (Sandbox Code Playgroud)

在我调用它之前,我通过获取它们来确保项目、数据集和表都存在。然后我使用表的实际 ID。


新信息:结果(如您所料) table.getId() 返回完全限定的 id,包括项目和数据集。当我对缩短的 id 进行硬编码以排除它们时,它起作用了。

Jor*_*ani 6

啊哈……好吧,这是预料之中的。该table.getId()调用将返回表的完全限定名称(即 project:dataset.table)。(请参阅 https://developers.google.com/bigquery/docs/reference/v2/tables)。

试试吧table.getTableReference().getTableId()。(请参阅https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/java/latest/com/google/api/services/bigquery/model/Table.html#getTableReference()

为什么,你可能会问?每个 REST 资源都应该有一个唯一的 ID。该表的完全限定唯一 ID 是 project:dataset.table 名称。但是,当您传递 id 的组件时,表组件只是该名称的最后一部分。