这个错误通常是由于在指定模式时使用了错误的类型标识符而导致的。应该使用正确的类型标识符来指定模式。以下是一个示例代码片段,演示了如何正确指定模式并写入到BigQuery表中。
from apache_beam.io.gcp.bigquery import parse_table_schema_from_json
# 模拟数据
data = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30}
]
# 定义模式
schema_json = '{"fields": [{"name": "name", "type": "STRING"}, {"name": "age", "type": "INTEGER"}]}'
schema = parse_table_schema_from_json(schema_json)
# 写入到BigQuery表
result = (
data
| beam.io.WriteToBigQuery(
table='mydataset.mytable',
schema=schema,
create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED,
write_disposition=beam.io.BigQueryDisposition.WRITE_APPEND
)
)