Python BigQuery API - 获取 table 架构
Python BigQuery API - get table schema
我正在尝试从 bigquery 获取模式 table。给定一个示例代码
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)
返回值如下:
[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.', ())]
我尝试仅以
格式捕获架构
'word' 'STRING','word_count' INTEGER'
有没有办法使用 API 调用或任何其他方法来获取此信息?
您始终可以获取 table.schema
变量并对其进行迭代,因为 table 是由 SchemaField 个值组成的列表:
result = ["{0} {1}".format(schema.name,schema.field_type) for schema in table.schema]
同一数据集的结果 table:
['word STRING', 'word_count INTEGER', 'corpus STRING', 'corpus_date INTEGER']
here、schema
已过期。
另一种方法是,在您拥有客户端和 table 实例之后,执行如下操作:
import io
f = io.StringIO("")
client.schema_to_json(table.schema, f)
print(f.getvalue())
我正在尝试从 bigquery 获取模式 table。给定一个示例代码
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)
返回值如下:
[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.', ())]
我尝试仅以
格式捕获架构'word' 'STRING','word_count' INTEGER'
有没有办法使用 API 调用或任何其他方法来获取此信息?
您始终可以获取 table.schema
变量并对其进行迭代,因为 table 是由 SchemaField 个值组成的列表:
result = ["{0} {1}".format(schema.name,schema.field_type) for schema in table.schema]
同一数据集的结果 table:
['word STRING', 'word_count INTEGER', 'corpus STRING', 'corpus_date INTEGER']
here、schema
已过期。
另一种方法是,在您拥有客户端和 table 实例之后,执行如下操作:
import io
f = io.StringIO("")
client.schema_to_json(table.schema, f)
print(f.getvalue())