Andrew Feather
Andrew Feather

Reputation: 183

Avro Writer in Python 3.5

I'm attempting to create an Avro based on an existing schema, but I'm getting an error while using the syntax from the tutorial located here. The console is saying the 'bytes' object has no attribute 'to_json', but it's deep within the Avro library. Are there any good workarounds or ways to fix this error?

Full error:

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'

Code:

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

Thanks for your help.

Upvotes: 1

Views: 601

Answers (1)

Andrew Feather
Andrew Feather

Reputation: 183

The documentation linked to in the question is out of date. I was able to fix the above by changing the schema method call to Parse and removing the b from "open".

Here's the updated code:

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

Upvotes: 1

Related Questions