[英]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.