1

I am looking for a way how the schema is set with a json file in Python on Big Query. The following document says I can set it with Schema field one by one, but I want to find out more efficient way. https://cloud.google.com/bigquery/docs/schemas

Autodetect would be skeptical to make it in this case. I will appreciate it if you helped me.

4

1 回答 1

3

您可以创建包含列/数据类型的 JSON 文件,并使用以下代码构建 BigQuery Schema。

JSON 文件 (schema.json):

[
    {
        "name": "emp_id",
        "type": "INTEGER"
    },
    {
        "name": "emp_name",
        "type": "STRING"
    }
]

蟒蛇代码:

import json
from google.cloud import bigquery

bigquerySchema = []
with open('schema.json') as f:
    bigqueryColumns = json.load(f)
    for col in bigqueryColumns:
        bigquerySchema.append(bigquery.SchemaField(col['name'], col['type']))

print(bigquerySchema)
于 2020-08-07T20:29:18.927 回答