2017-02-27 69 views
1

我试图创建一个基于现有模式的Avro,但我在使用教程位于here的语法时遇到错误。控制台说'字节'对象没有'to_json'属性,但它在Avro库中很深。有没有什么好的解决方法或方法来解决这个错误?在Python 3.5 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) 

感谢您的帮助。

回答

0

链接到in the question的文档已过时。我可以通过将模式方法调用更改为Parse并从“打开”中删除b来解决上述问题。

下面是更新后的代码:

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