[英]Python BigQuery API - get table schema
I am trying to fetch schema form bigquery table. 我试图获取架构形式的bigquery表。 Given a sample code like 给定一个示例代码,例如
from google.cloud import bigquery
from google.cloud import storage
client = bigquery.Client.from_service_account_json('service_account.json')
def test_extract_schema(client):
project = 'bigquery-public-data'
dataset_id = 'samples'
table_id = 'shakespeare'
dataset_ref = client.dataset(dataset_id, project=project)
table_ref = dataset_ref.table(table_id)
table = client.get_table(table_ref) # API Request
# View table properties
print(table.schema)
if __name__ == '__main__':
test_extract_schema(client)
This is returning value like: 这是返回值,例如:
[SchemaField('word', 'STRING', 'REQUIRED', 'A single unique word (where whitespace is the delimiter) extracted from a corpus.', ()), SchemaField('word_count', 'INTEGER', 'REQUIRED', 'The number of times this word appears in this corpus.', ()), SchemaField('corpus', 'STRING', 'REQUIRED', 'The work from which this word was extracted.', ()), SchemaField('corpus_date', 'INTEGER', 'REQUIRED', 'The year in which this corpus was published.', ())]
Where I am trying to capture schema only in the format like 我试图仅以以下格式捕获模式的地方
'word' 'STRING','word_count' INTEGER'
Is there any way to get this using API call or any other method? 有什么方法可以使用API调用或任何其他方法来获得此信息?
You can always get the table.schema
variable and iterate over it, since the table is a list made of SchemaField values: 您始终可以获取table.schema
变量并对其进行迭代,因为该表是由SchemaField值组成的列表:
result = ["{0} {1}".format(schema.name,schema.field_type) for schema in table.schema]
Result for that same dataset and table: 相同数据集和表的结果:
['word STRING', 'word_count INTEGER', 'corpus STRING', 'corpus_date INTEGER']
在这里 , schema
已过期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.