I am having an issue with Bigquery Python API. Here is the stack trace when I execute my script:
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.
The script is very simple, open a schema file and create the table:
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))
When I execute the schema in the console and the CLI, the table is created perfectly as is. How can the console and CLI execute the create table but chokes in the API. I have searched and searched and have not found an answer. Can someone help?
Here is the schema stored in the hcl-schema.json file. I've shortened the attribut list for brevity, but otherwise intact:
[
{
"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
Dazed and confused