相关问题:Bigquery使用BQ命令行工具将列添加到表模式
我想在BigQuery中使用BigQuery Python向现有表添加一个新列(更新现有表的模式)。
然而,我的代码似乎不起作用。
这是我的密码:
flow = flow_from_clientsecrets('secret_key_path', scope='my_scope')
storage = Storage('CREDENTIAL_PATH')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, tools.argparser.parse_args([]))
http = httplib2.Http()
http = credentials.authorize(http)
bigquery_service = build('bigquery', 'v2', http=http)
tbObject = bigquery_service.tables()
query_body = {'schema': {'name':'new_column_name', 'type':'STRING'}}
tbObject.update(projectId='projectId', datasetId='datasetId', tableId='tableId', body=query_body).execute()它返回Provided schema doesn't match existing table's schema错误。有人能给我举个Python例子吗?非常感谢!
发布于 2016-03-29 22:27:45
我的评论摘要(因为我现在有几分钟的时间):
发布于 2016-03-29 21:57:09
基于Mikhail Berlyant注释,我必须将带有新字段(列)的现有表的模式传递给update()方法,以更新现有表的模式。
下面给出了python代码示例:
...
tbObject = bigquery_service.tables()
# get current table schema
table_data = tbObject.get(projectId=projectId, datasetId=datasetId, tableId=tableId).execute()
schema = table_data.get('schema')
new_column = {'name': 'new_column_name', 'type': 'STRING'}
# append new field to current table's schema
schema.get('fields').append(new_column)
query_body = {'schema': schema}
tbObject.update(projectId='projectId', datasetId='datasetId', tableId='tableId', body=query_body).execute()而且,也无法为现有的行(表)设置新列的值。感谢Mikhail Berlyant建议,为现有行设置值的方法是为具有值的新列创建一个单独的表,并将现有表与该表连接起来,以替换旧的模式表。
https://stackoverflow.com/questions/36295488
复制相似问题