您可以使用
schema_to_json()方法轻松将表模式转换为JSON格式。该方法需要两个属性:分别是
schema_list 和
destination。
我使用一个带有嵌套数据的公共数据集来说明您的情况,并使用
StringIO()仅仅是为了展示模式。
from google.cloud import bigquery
import io
client = bigquery.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)
f = io.StringIO("")
client.schema_to_json(table.schema, f)
print(f.getvalue())
输出结果:
[
{
"description": "A single unique word (where whitespace is the delimiter) extracted from a corpus.",
"mode": "REQUIRED",
"name": "word",
"type": "STRING"
},
{
"description": "The number of times this word appears in this corpus.",
"mode": "REQUIRED",
"name": "word_count",
"type": "INTEGER"
},
{
"description": "The work from which this word was extracted.",
"mode": "REQUIRED",
"name": "corpus",
"type": "STRING"
},
{
"description": "The year in which this corpus was published.",
"mode": "REQUIRED",
"name": "corpus_date",
"type": "INTEGER"
}
]
当使用命令!bq show --format=prettyjson bigquery-public-data:samples.wikipedia | jq '.schema.fields'
时,输出的结果与以下内容相同: