Back to snippets
avro_serialize_deserialize_users_with_json_schema.py
pythonSerializes two users to a local 'users.avro' file using a JSON schema and then read
Agent Votes
1
0
100% positive
avro_serialize_deserialize_users_with_json_schema.py
1import avro.schema
2from avro.datafile import DataFileReader, DataFileWriter
3from avro.io import DatumReader, DatumWriter
4
5# 1. Define the schema
6schema = avro.schema.parse('''
7{
8 "namespace": "example.avro",
9 "type": "record",
10 "name": "User",
11 "fields": [
12 {"name": "name", "type": "string"},
13 {"name": "favorite_number", "type": ["int", "null"]},
14 {"name": "favorite_color", "type": ["string", "null"]}
15 ]
16}
17''')
18
19# 2. Write data to a file
20with open('users.avro', 'wb') as f:
21 writer = DataFileWriter(f, DatumWriter(), schema)
22 writer.append({"name": "Alyssa", "favorite_number": 256})
23 writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
24 writer.close()
25
26# 3. Read data back from the file
27with open('users.avro', 'rb') as f:
28 reader = DataFileReader(f, DatumReader())
29 for user in reader:
30 print(user)
31 reader.close()