Python BigQuery API - 获取 table schema/header
Python BigQuery API - get table schema/header
给定一个查询示例
import uuid
from google.cloud import bigquery
def query_shakespeare():
client = bigquery.Client()
query_job = client.run_async_query(str(uuid.uuid4()), """
#standardSQL
SELECT corpus AS title, COUNT(*) AS unique_words
FROM `publicdata.samples.shakespeare`
GROUP BY title
ORDER BY unique_words DESC
LIMIT 10""")
query_job.begin()
query_job.result() # Wait for job to complete.
destination_table = query_job.destination
destination_table.reload()
for row in destination_table.fetch_data():
print(row)
if __name__ == '__main__':
query_shakespeare()
如何获取 table 的架构?
行,在前面的例子中有形式
Row(('august', -1, 'aaa', 333), {'col1': 0, 'col2': 1, 'col3': 2})
但是我找不到,对于包google-cloud-bigquery==0.28.0,解压headerJSON的方法。
当然,table 模式的提取对我来说也很好,但是当前的 Google 文档看起来不适用于最新版本...
如果需要刚才查询的table的schema,可以从QueryJob
:
的result
方法获取
client = bq.Client()
query = """
#standardSQL
SELECT corpus AS title, COUNT(*) AS unique_words
FROM `publicdata.samples.shakespeare`
GROUP BY title
ORDER BY unique_words DESC
LIMIT 10"""
query_job = client.query(query)
result = query_job.result()
schema = result.schema
结果:
[SchemaField(u'title', u'string', u'NULLABLE', None, ()),
SchemaField(u'unique_words', u'integer', u'NULLABLE', None, ())]
(您在问题中提供的代码与版本 0.27
相关)。
关于获取 header JSON 的问题,不确定我是否理解正确,但看起来你需要架构来找到 json 所在的位置(我猜这里)。
table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref)
original_schema = table.schema
此代码将打印 table 架构。就我而言,它遵循输出
[SchemaField('guidislink', 'STRING', 'NULLABLE', None, ()),
SchemaField('id', 'STRING', 'NULLABLE', None, ()),
SchemaField('link', 'STRING', 'NULLABLE', None, ())]
给定一个查询示例
import uuid
from google.cloud import bigquery
def query_shakespeare():
client = bigquery.Client()
query_job = client.run_async_query(str(uuid.uuid4()), """
#standardSQL
SELECT corpus AS title, COUNT(*) AS unique_words
FROM `publicdata.samples.shakespeare`
GROUP BY title
ORDER BY unique_words DESC
LIMIT 10""")
query_job.begin()
query_job.result() # Wait for job to complete.
destination_table = query_job.destination
destination_table.reload()
for row in destination_table.fetch_data():
print(row)
if __name__ == '__main__':
query_shakespeare()
如何获取 table 的架构? 行,在前面的例子中有形式
Row(('august', -1, 'aaa', 333), {'col1': 0, 'col2': 1, 'col3': 2})
但是我找不到,对于包google-cloud-bigquery==0.28.0,解压headerJSON的方法。 当然,table 模式的提取对我来说也很好,但是当前的 Google 文档看起来不适用于最新版本...
如果需要刚才查询的table的schema,可以从QueryJob
:
result
方法获取
client = bq.Client()
query = """
#standardSQL
SELECT corpus AS title, COUNT(*) AS unique_words
FROM `publicdata.samples.shakespeare`
GROUP BY title
ORDER BY unique_words DESC
LIMIT 10"""
query_job = client.query(query)
result = query_job.result()
schema = result.schema
结果:
[SchemaField(u'title', u'string', u'NULLABLE', None, ()),
SchemaField(u'unique_words', u'integer', u'NULLABLE', None, ())]
(您在问题中提供的代码与版本 0.27
相关)。
关于获取 header JSON 的问题,不确定我是否理解正确,但看起来你需要架构来找到 json 所在的位置(我猜这里)。
table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref)
original_schema = table.schema
此代码将打印 table 架构。就我而言,它遵循输出
[SchemaField('guidislink', 'STRING', 'NULLABLE', None, ()),
SchemaField('id', 'STRING', 'NULLABLE', None, ()),
SchemaField('link', 'STRING', 'NULLABLE', None, ())]