bqpython怎么用_BigQuery:使用python BQ API将新列添加到现有表

I want to add a new column to existing tables (update the existing table's schema) in BigQuery using BigQuery Python API.

However my code seems not working.

Here's my code:

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()

it returns Provided schema doesn't match existing table's schema error.

Can anyone give me a working Python example?

Many thanks!

解决方案

summary of my comments (as i've got some minutes now for this):

whole schema (along with new field) needs to be supplied to api

new field will be added with null for existing rows. no way to set

value

you can have some logic in queries that you will be running against

this table to compensate this. or you can have separate table with

just this new field and some key that you will be joining your

existing table with new table to get this field

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值