当 json 字符串作为变量传递时,Python Avro writer.append 不起作用。

Python Avro writer.append doesn't work when a json string is passed as a variable.

Avro 架构文件:user.avsc

{"namespace": "example.avro",
 "type": "record",
 "name": "User",
 "fields": [
     {"name": "TransportProtocol", "type": "string"}
 ]
}

粘贴我的有效代码片段:-

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()

粘贴无效的代码片段:-

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()

当我按附加函数中的原样传递 json 字符串时,它会说出我得到所需的 avro 输出。但是,如果我将 json 字符串初始化为一个变量,然后尝试在 append 函数中传递该变量,它不起作用并抛出错误:-

avro.io.AvroTypeException: The datum {"TransportProtocol": "udp"} is not an example of the schema {

有帮助吗?谢谢

我认为这可能是因为在您的第一个示例中您实际上传递了字典 {"TransportProtocol": "udp"},而不是字符串。但是在第二个中,您传递了一个字符串 '{"TransportProtocol": "udp"}'

检查一下 (http://avro.apache.org/docs/1.7.6/gettingstartedpython.html):

We use DataFileWriter.append to add items to our data file. Avro records are represented as Python dicts.

所以基本上,您不能将字符串作为参数传递。