【发布时间】:2016-08-17 05:59:29
【问题描述】:
Avro 架构文件:user.avsc
{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "TransportProtocol", "type": "string"}
]
}
粘贴我的代码 sn-p 有效:-
import json
from avro import schema, datafile, io
import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
schema = avro.schema.parse(open("user.avsc").read())
writer = DataFileWriter(open("users.avro", "w"), DatumWriter(), schema)
writer.append({"TransportProtocol": "udp"})
writer.close()
粘贴我的代码 sn-p 不起作用:-
dummy_json = '{"TransportProtocol": "udp"}'
schema = avro.schema.parse(open("user.avsc").read())
writer = DataFileWriter(open("users.avro", "w"), DatumWriter(), schema)
writer.append(dummy_json)
writer.close()
当我在 append 函数中传递 json 字符串时,它会输出并得到所需的 avro 输出。但是如果我将 json 字符串初始化为一个变量,然后尝试在 append 函数中传递该变量,它就不起作用并抛出一个错误:-
avro.io.AvroTypeException: The datum {"TransportProtocol": "udp"} is not an example of the schema {
有什么帮助吗?谢谢
【问题讨论】: