public
Last active

  • Download Gist
avro_understanding_1
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
import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
import simplejson as json
 
a = avro.schema.parse(json.dumps({"name": "hey", "type": "record", "fields": [{"name":"n", "type":"double"}]}))
b = avro.schema.parse(json.dumps({"name": "hey", "type": "record", "fields": [{"name":"n", "type":"int"}]}))
c = avro.schema.parse(json.dumps({"name": "hey", "type": "record", "fields": [{"name":"n", "type":["int","double"]}]}))
writer = DataFileWriter(open("a.avro", "w"), DatumWriter(), a)
writer.append({"n": 1.0})
writer.append({"n": 1})
writer.close()
 
reader = DataFileReader(open("a.avro", "r"), DatumReader())
print reader.next() # {u'n': 1.0}
print reader.next() # {u'n': 1.0}
 
reader = DataFileReader(open("a.avro", "r"), DatumReader(readers_schema=c))
print reader.next() # {u'n': 1.0}
print reader.next() # {u'n': 1.0}
 
d = avro.schema.parse(json.dumps({"name": "hey", "type": "record", "fields": [{"name":"n", "type":["int", "double"]}]}))
writer = DataFileWriter(open("d.avro", "w"), DatumWriter(), d)
writer.append({"n": 1.0})
writer.append({"n": 1})
writer.close()
 
reader = DataFileReader(open("d.avro", "r"), DatumReader())
print reader.next() # {u'n': 1.0}
print reader.next() # {u'n': 1.0}
 
reader = DataFileReader(open("d.avro", "r"), DatumReader(readers_schema=b))
print reader.next() # error, schema doesn't matdh
print reader.next() # {u'n': 0} ???
print reader.next() # {u'n': 0} ???
 
reader = DataFileReader(open("d.avro", "r"), DatumReader(readers_schema=c))
print reader.next() # {u'n': 1.0}
print reader.next() # {u'n': 1.0}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.