-
Notifications
You must be signed in to change notification settings - Fork 0
/
avro_writier.py
55 lines (43 loc) · 2.02 KB
/
avro_writier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import avro
from avro.datafile import DataFileWriter
from avro.io import DatumWriter
if __name__ == '__main__':
# write with schema v1
schema_file = 'schemas/schema_v1.json'
avro_file = 'data/users_v1.avro'
with open(schema_file) as f:
schema = avro.schema.Parse(f.read())
with DataFileWriter(open(avro_file, 'wb'), DatumWriter(), schema) as dfw:
dfw.append({"name": "Alyssa", "favorite_color": "red"})
dfw.append({"name": "Ben"})
dfw.append({"name": "Tim", "favorite_color": "green"})
try:
dfw.append({"favorite_color": "emerald"})
except avro.io.AvroTypeException:
pass
# write with schema v2
schema_file = 'schemas/schema_v2_add_field.json'
avro_file = 'data/users_v2.avro'
with open(schema_file) as f:
schema = avro.schema.Parse(f.read())
with DataFileWriter(open(avro_file, 'wb'), DatumWriter(), schema) as dfw:
dfw.append({"name": "Alyssa", "age": 2, "favorite_color": "red"})
dfw.append({"name": "Ben", "age": 99, "favorite_color": "Blue"})
dfw.append({"name": "Tim", "age": 10})
# write with both schema v1 and v2 in the same file
avro_file = 'data/users_v1_and_v2.avro'
schema_file = 'schemas/schema_v1.json'
with open(schema_file) as f:
schema = avro.schema.Parse(f.read())
with DataFileWriter(open(avro_file, 'wb'), DatumWriter(), schema) as dfw:
dfw.append({"name": "Alyssa", "favorite_color": "red"})
dfw.append({"name": "Ben"})
dfw.append({"name": "Tim", "favorite_color": "green"})
schema_file = 'schemas/schema_v2_add_field.json'
with open(schema_file) as f:
schema = avro.schema.Parse(f.read())
# note: this will update the schema in the avro file
with DataFileWriter(open(avro_file, 'wb'), DatumWriter(), schema) as dfw:
dfw.append({"name": "Alyssa", "age": 2, "favorite_color": "red"})
dfw.append({"name": "Ben", "age": 99, "favorite_color": "Blue"})
dfw.append({"name": "Tim", "age": 10})