这个错误通常表示Avro模式中有一个不可哈希类型的嵌套字典。在处理嵌套字典时,需要将其转换为可哈希类型,例如元组。以下是一个示例,使用元组替换字典来解决这个问题:
import avro.schema
# 创建一个包含不可哈希类型字典的avro模式
schema_dict = {
"namespace": "example.avro",
"type": "record",
"name": "user",
"fields": [
{"name": "name", "type": "string"},
{"name": "address", "type": {
"type": "record",
"name": "mailing_address",
"fields": [
{"name": "street", "type": "string"},
{"name": "city", "type": "string"},
{"name": "state", "type": "string"},
{"name": "zip", "type": "int"}
]
}},
{"name": "phone", "type": {"type": "map", "values": "string"}}
]
}
# 转换不可哈希类型字典为元组
schema_tuple = (
"example.avro",
"record",
"user",
[
("name", "string"),
("address", ("record", "mailing_address", [
("street", "string"),
("city", "string"),
("state", "string"),
("zip", "int")
])),
("phone", ("map", "string"))
]
)
# 使用转换后的元组创建avro模式
schema = avro.schema.make_avsc_object(schema_tuple)