我在使用 Bigquery Python API 时遇到问题。这是我执行脚本时的堆栈跟踪:
Traceback (most recent call last):
File "createTable.py", line 17, in <module>
open_schema()
File "createTable.py", line 12, in open_schema
table = bigquery.Table(table_id, schema=schema)
...
"Schema items must either be fields or compatible "
ValueError: Schema items must either be fields or compatible mapping representations.
该脚本非常简单,打开一个架构文件并创建表:
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
table_id = "project-py-290522:bq_dts.bq-test"
def open_schema():
with open("hcl-schema.json","r", encoding = "utf-8") as fName:
schema = fName.readlines()
table = bigquery.Table(table_id, schema=schema)
print(repr(table))
client.create_table(table) # Make an API request.
if __name__ == "__main__":
open_schema()
print("Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id))
当我在控制台和 CLI 中执行架构时,表将按原样完美创建。控制台和CLI如何执行创建表但在API中卡住。我搜索了又搜索,却没有找到答案。有人可以帮忙吗?
以下是存储在 hcl-schema.json 文件中的架构。为了简洁起见,我缩短了属性列表,但其他方面保持不变:
[
{
"name":"user_id",
"type":"STRING",
"mode":"NULLABLE"
},
{
"name":"msg_version",
"type":"STRING",
"mode":"REQUIRED"
},
{
"name":"APIStreamData",
"type":"RECORD",
"mode":"REQUIRED",
"fields":
[
{
"name":"msg_version",
"type":"STRING",
"mode":"REQUIRED"
},
{
"name":"streams",
"type":"RECORD",
"mode":"REPEATED",
"fields":
[
{
"name":"length",
"type":"STRING",
"mode":"REQUIRED"
},
{
"name":"cached",
"type":"STRING",
"mode":"NULLABLE"
},
{
"name":"track",
"type":"RECORD",
"mode":"REQUIRED",
"fields":
[
{
"name":"msg_version",
"type":"STRING",
"mode":"REQUIRED"
},
{
"name":"track_id",
"type":"STRING",
"mode":"REQUIRED"
}
]
}
]
}
]
}
]
Thanks
茫然和困惑