【问题标题】:BigQuery 'get_client' package missing/not importable on Python 3 (to export a schema)BigQuery 'get_client' 包在 Python 3 上丢失/不可导入(导出架构)
【发布时间】:2019-07-26 21:50:04
【问题描述】:

编辑:我已经安装了 BigQuery-Python、bigQueryExporter 和 google-cloud-bigquery,所以这不是问题。

我正在尝试导出现有 BQ 表的架构,包括所有嵌套字段。为此,我试图导入 BQ 的“get_client”包,但它似乎不存在于 python3 中(或者它可能已经改变)。

我第一次尝试创建架构是成功的,但没有处理我需要的嵌套字段(见下文):

from google.cloud import bigquery


def test_extract_schema():

    client = bigquery.Client.from_service_account_json('file')
    project = 'project_id'
    dataset_id = 'test1'
    table_id = 'testc'

    dataset_ref = client.dataset(dataset_id, project=project)
    table_ref = dataset_ref.table(table_id)
    table = client.get_table(table_ref)  # API Request

    result = {schema.name: schema.field_type for schema in table.schema}
    listresult = [result]

    print(listresult)


if __name__ == '__main__':
    test_extract_schema()

我当前的版本,即使我让包运行,我也不确定它是否会工作,看起来像这样:

import web
from google.cloud import bigquery
from bigquery import get_client


urls = ('/GetFields(.*)', 'get_Fields')
app = web.application(urls, globals())


def get():

    project_id = 'project_id'
    dataset_id = 'test1'
    table_id = 'testc'

    client = bigquery.Client.from_service_account_json('file')

    results = client.get_table_schema(dataset_id, table_id)

    return results


if __name__ == "__main__":
    get()

“get_table_schema”无法识别,因为无法安装 get_client 包。这不适用于 Python 3,还是有其他问题?提前致谢。

【问题讨论】:

    标签: python google-bigquery schema


    【解决方案1】:

    解决了我自己的问题!

    from google.cloud import bigquery
    
    
    def get_table_schema():
    
        project_id = 'project_id'
        dataset_id = 'test1'
        table_id = 'testc'
    
        bigquery_client = bigquery.Client.from_service_account_json('file')
    
        dataset_ref = bigquery_client.dataset(dataset_id)
        bq_tableref = bigquery.table.TableReference(dataset_ref, table_id)
        bq_table = bigquery_client.get_table(bq_tableref)
    
        schema = [i.to_api_repr() for i in bq_table.schema]
    
        print('schema is ', schema)
    
        return schema
    
    
    if __name__ == "__main__":
        get_table_schema()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-12
      • 2017-12-10
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2012-06-26
      相关资源
      最近更新 更多