21

我正在尝试将新列添加到 BigQuery 现有表。我尝试过 bq 命令工具和 API 方法。调用 Tables.update() 时出现以下错误。

我尝试提供带有附加字段的完整架构,这也给了我同样的错误,如下所示。

使用 API 我得到以下错误:

{
    "schema": {
        "fields": [{
            "name": "added_column",
            "type": "integer",
            "mode": "nullable"
        }]
    }
}



{
    "error": {
        "errors": [{
            "domain": "global",
            "reason": "invalid",
            "message": "Provided Schema does not match Table [blah]"
        }],
        "code": 400,
        "message": "Provided Schema does not match Table [blah]"
    }
}

使用 BQ 工具出现以下错误:

./bq update -t blah added_column:integer

更新操作中的 BigQuery 错误:提供的架构与表不匹配 [blah]

4

5 回答 5

45

尝试这个:

bq --format=prettyjson show yourdataset.yourtable > table.json

编辑 table.json 并删除除“字段”内部之外的所有内容(例如保留[ { "name": "x" ... }, ... ])。然后将您的新字段添加到架构中。

或通过jq管道

bq --format=prettyjson show yourdataset.yourtable | jq .schema.fields > table.json

然后运行:

bq update yourdataset.yourtable table.json

您可以添加--apilog=apilog.txt到命令行的开头,它将准确显示从 bigquery 服务器发送/返回的内容。

于 2013-05-23T01:01:47.263 回答
4

在我的情况下,我试图向REQUIRED模板表添加一个字段,并且遇到了这个错误。将字段更改为NULLABLE,让我更新表格。

还有更新的最新版本,适用于任何从谷歌绊倒的人。

#To create table
bq mk --schema domain:string,pageType:string,source:string -t Project:Dataset.table
#Or using schema file
bq mk --schema SchemaFile.json -t Project:Dataset.table


#SchemaFile.json format
[{                                                                                                                                                                                                                                                
  "mode": "REQUIRED",
  "name": "utcTime",
  "type": "TIMESTAMP"
},    
{
  "mode": "REQUIRED",
  "name": "domain",
  "type": "STRING"
},  
{
  "mode": "NULLABLE",
  "name": "testBucket",
  "type": "STRING"
},  
{
  "mode": "REQUIRED",
  "name": "isMobile",
  "type": "BOOLEAN"                                                                                                                                                                                                                       
},
{
  "mode": "REQUIRED",
  "name": "Category",
  "type": "RECORD",
  "fields": [
    {
      "mode": "NULLABLE",
      "name": "Type",
      "type": "STRING"
     },
     {
       "mode": "REQUIRED",
       "name": "Published",
       "type": "BOOLEAN"
     }
    ]
}]

# TO update
bq update --schema UpdatedSchema.json -t Project:Dataset.table
# Updated Schema contains old and any newly added columns 

模板表的一些文档

于 2016-05-03T18:36:18.983 回答
3

使用 BigQuery Node JS API 的示例:

const fieldDefinition = {
    name: 'nestedColumn',
    type: 'RECORD',
    mode: 'REPEATED',
    fields: [
        {name: 'id', type: 'INTEGER', mode: 'NULLABLE'},
        {name: 'amount', type: 'INTEGER', mode: 'NULLABLE'},
    ],
}; 

const table = bigQuery.dataset('dataset1').table('source_table_name');
const metaDataResult = await table.getMetadata();
const metaData = metaDataResult[0];

const fields = metaData.schema.fields;
fields.push(fieldDefinition);

await table.setMetadata({schema: {fields}});
于 2018-11-13T10:20:19.507 回答
2

我被困在尝试使用 Python 客户端将列添加到 BigQuery 中的现有表中,并多次找到这篇文章。然后,如果有人遇到同样的问题,我会让为我解决它的代码:

# update table schema
bigquery_client = bigquery.Client()
dataset_ref = bigquery_client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
table = bigquery_client.get_table(table_ref)
new_schema = list(table.schema)
new_schema.append(bigquery.SchemaField('LOLWTFMAN','STRING'))
table.schema = new_schema
table = bigquery_client.update_table(table, ['schema'])  # API request
于 2018-03-13T19:41:43.273 回答
0

您可以通过 GCP 控制台将 Schema 添加到您的表中更简单明了:-

将架构添加到您的表

于 2020-06-14T16:16:15.023 回答