Python 3.5中的Avro Writer

时间:2017-02-27 15:50:33

标签: python apache python-3.x avro

我尝试根据现有架构创建Avro,但在使用位于here的教程中的语法时,我收到了错误消息。控制台正在说'字节' object没有属性' to_json',但它在Avro库中很深。是否有任何良好的解决方法或方法来解决此错误?

完整错误:

Traceback (most recent call last):
File "build_data.py", line 15, in <module>
al.create_avro(logs)
File "AppLog.py", line 57, in create_avro
writer = DataFileWriter(open("new.avro", "wb"), DatumWriter(), schema)
File "/usr/local/lib/python3.5/dist-packages/avro_python3-1.8.1-py3.5.egg/avro/datafile.py", line 151, in __init__
self.SetMeta('avro.schema', str(writer_schema).encode('utf-8'))
File "/usr/local/lib/python3.5/dist-packages/avro_python3-1.8.1-py3.5.egg/avro/schema.py", line 266, in __str__
return json.dumps(self.to_json())
File "/usr/local/lib/python3.5/dist-packages/avro_python3-1.8.1-py3.5.egg/avro/schema.py", line 808, in to_json
to_dump['items'] = item_schema.to_json(names)
AttributeError: 'bytes' object has no attribute 'to_json'

代码:

schema = avro.schema.ArraySchema(open("AppLogs.avsc", "rb").read())
writer = DataFileWriter(open("new.avro", "wb"), DatumWriter(), schema)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

image关联的文档已过期。我可以通过将架构方法调用更改为Parse并从&#34;打开&#34;中删除b来解决上述问题。

这是更新后的代码:

schema = avro.schema.Parse(open("AppLogs.avsc", "r").read())
writer = DataFileWriter(open(new_avro, "wb"), DatumWriter(), schema)