使用load_table_from_storage
将Google云端存储上托管的json上传到Bigquery时,指定架构的最佳方法是什么?
字段列表非常复杂,我已经有了这样的格式: https://cloud.google.com/bigquery/docs/personsDataSchema.json
有什么方法可以在Python中以这种格式提供架构?如果是的话,我应该使用什么语法?到目前为止,我尝试过各种各样的选项。
答案 0 :(得分:1)
请注意load_table_from_storage在destination
输入中收到类google.cloud.bigquery.table.Table
的对象。这是您应该指定架构的地方。
例如,如果“bqc”是您的BigQuery Client对象,则会创建一个Table对象:
ds = bqc.dataset('dataset_name')
table = ds.table('table_name')
现在假设您在json文件中有这些数据可以使用:
{"user_id": "1", "visitid": 1, "hits": [{"hitNumber": 1, "type": "PAGE"}, {"hitNumber": 2, "type": "PAGE"}]}
{"user_id": "2", "visitid": 1, "hits": [{"hitNumber": 1, "type": "EVENT"}, {"hitNumber": 2, "type": "PAGE"}]}
然后定义它的模式就像这样:
from google.cloud.bigquery.schema import SchemaField
f1 = SchemaField('user_id', 'STRING')
f2 = SchemaField('visitid', 'INTEGER')
f3 = SchemaField('hits', 'RECORD', mode='REPEATED', fields=[SchemaField('hitNumber', 'INTEGER'), SchemaField('type', 'STRING')])
table.schema = [f1, f2, f3]
table.create()