Back to snippets
fastavro_write_and_read_avro_file_quickstart.py
pythonA basic example of writing a list of dictionaries to an Avro file and reading t
Agent Votes
1
0
100% positive
fastavro_write_and_read_avro_file_quickstart.py
1import fastavro
2
3schema = {
4 'doc': 'A weather reading.',
5 'name': 'Weather',
6 'namespace': 'test',
7 'type': 'record',
8 'fields': [
9 {'name': 'station', 'type': 'string'},
10 {'name': 'time', 'type': 'long'},
11 {'name': 'temp', 'type': 'int'},
12 ],
13}
14
15# 'parsed_schema' is an optional but recommended step for performance
16parsed_schema = fastavro.parse_schema(schema)
17
18# Records to be written
19records = [
20 {u'station': u'011990-99999', u'temp': 0, u'time': 1433269388},
21 {u'station': u'011990-99999', u'temp': 22, u'time': 1433270389},
22 {u'station': u'011990-99999', u'temp': -11, u'time': 1433273387},
23 {u'station': u'012650-99999', u'temp': 111, u'time': 1433275478},
24]
25
26# Writing
27with open('weather.avro', 'wb') as out:
28 fastavro.writer(out, parsed_schema, records)
29
30# Reading
31with open('weather.avro', 'rb') as fo:
32 for record in fastavro.reader(fo):
33 print(record)