如果可以使用 bq 命令行就可以了。
假设您的 JSON 文件 (my_json_file.json) 位于 GCS 存储桶(例如 my_gcs_bucket)和目标表 my_dataset.my_destination_table 中,您可以运行以下命令
bq load --ignore_unknown_values --source_format=NEWLINE_DELIMITED_JSON my_dataset.my_destination_table "gs://my_gcs_bucket/my_json_file.json" ./schema.json
在 schema.json 中,您已经选择了目标表的架构。例如,以下两个模式将按预期加载数据:
schema_1.json
[
{
"mode": "NULLABLE",
"name": "Key1",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "Key2",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "Key3",
"type": "STRING"
}
]
和schema_2.json
[
{
"mode": "NULLABLE",
"name": "Key1",
"type": "STRING"
},
{
"fields": [
{
"mode": "NULLABLE",
"name": "SubKey1",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "SubKey2",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "SubKey3",
"type": "STRING"
}
],
"mode": "REPEATED",
"name": "List1",
"type": "RECORD"
}
]
然后
bq load --ignore_unknown_values --source_format=NEWLINE_DELIMITED_JSON my_dataset.my_destination_table_1 "gs://my_gcs_bucket/my_json_file.json" ./schema_1.json
bq load --ignore_unknown_values --source_format=NEWLINE_DELIMITED_JSON my_dataset.my_destination_table_2 "gs://my_gcs_bucket/my_json_file.json" ./schema_2.json
将基于同一个 JSON 文件加载两个不同的表