- First wget the latest release from here
- Go to the py3 subfolder under lang and build the project via your python 3 (read more here)
Create user.avsc in the same folder as the python code is going to be located.
The schema:
{ "namespace": "example.avro", "type": "record", "name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]} ] }
Create the .py from the code provided below (Mind that I had to make minor changes to the code in here to get this to work).
The modified code is as follows.
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", "wb"), DatumWriter(), schema) writer.append({"name": "Alyssa", "favorite_number": 256}) writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"}) writer.close() reader = DataFileReader(open("users.avro", "rb"), DatumReader()) for user in reader: print(user) reader.close()
Starting with Python 3 and Avro 1.7.6, what are the steps? (Q&A)
-
13-10-2022 - |
Question
This is a Q&A, meaning I am sharing my solution/answer to a problem I faced:
The problem was that the getting started guide from the apache site was not entirely up- -to-date and after a bit of fiddling I manage to get the sample to work.
- First wget the latest release from here
- Go to the py3 subfolder under lang and build the project via your python 3 (read more here)
Create user.avsc in the same folder as the python code is going to be located.
The schema:
{ "namespace": "example.avro", "type": "record", "name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]} ] }
Create the .py from the code provided below (Mind that I had to make minor changes to the code in here to get this to work).
The modified code is as follows.
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", "wb"), DatumWriter(), schema) writer.append({"name": "Alyssa", "favorite_number": 256}) writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"}) writer.close() reader = DataFileReader(open("users.avro", "rb"), DatumReader()) for user in reader: print(user) reader.close()
Solution
OTHER TIPS
You can create a new project and then set python 2.7 as system interpreter and then import avro.
i got the output as in documentation https://avro.apache.org/docs/1.8.2/gettingstartedpython.html
{u'favorite_color': None, u'favorite_number': 256, u'name': u'Alyssa'}
{u'favorite_color': u'red', u'favorite_number': 7, u'name': u'Ben'}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow