API将新列添加到现有表

API将新列添加到现有表

本文介绍了BigQuery:使用python BQ API将新列添加到现有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关问题:。

然而,我的代码似乎不工作。



这是我的代码:

  flow = flow_from_clientsecrets('secret_key_path',scope ='my_scope')
storage = Storage('CREDENTIAL_PATH')
credentials = storage.get()
如果凭证是None或凭证.invalid:
credentials = tools.run_flow(flow,storage,tools.argparser.parse_args([]))
http = httplib2.Http()
http = credentials.authorize(http)
bigqu red_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()

它返回提供的模式与现有表模式错误不匹配。
任何人都可以给我一个有效的Python例子吗?
非常感谢!

解决方案

我的评论摘要(因为我现在有几分钟了) / p>


  • 整个模式(以及新字段)需要提供给api

  • 新字段将会为现有行添加空值。没有办法设置
    值您可以在查询中使用某些逻辑,您将针对
    此表执行补偿操作。或者你可以使用单独的表格与
    只是这个新字段和一些你将加入你的
    现有表格与新表格来获得这个字段的键

    Related question: Bigquery add columns to table schema using BQ command line tools

    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 setvalue
    • you can have some logic in queries that you will be running againstthis table to compensate this. or you can have separate table withjust this new field and some key that you will be joining yourexisting table with new table to get this field

    这篇关于BigQuery:使用python BQ API将新列添加到现有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:32