簡體   English   中英

BigQuery'get_client'包丟失/不可在Python 3上導入(以導出架構)

[英]BigQuery 'get_client' package missing/not importable on Python 3 (to export a schema)

編輯:我已經安裝了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,還是存在其他問題? 提前致謝。

解決了我自己的問題!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM