Python BigQuery API - get table schema

蹲街弑〆低调 提交于 2019-12-12 09:46:38

问题


I am trying to fetch schema form bigquery table. 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?


回答1:


You can always get the table.schema variable and iterate over it, since the table is a list made of SchemaField values:

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']



回答2:


here, schema has been expired.




回答3:


An alternative is, after you have your client and table instances, to do something like this:

import io
f = io.StringIO("")
client.schema_to_json(table.schema, f)
print(f.getvalue())


来源:https://stackoverflow.com/questions/50885946/python-bigquery-api-get-table-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!