Created
November 18, 2015 09:16
-
-
Save oreh/38ac3543923c619710a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import json | |
import random | |
import avro.io | |
import avro.schema | |
schema_str='''{"namespace": "example.avro", | |
"type": "record", | |
"name": "User", | |
"fields": [ | |
{"name": "name", "type": "string"}, | |
{"name": "favorite_number", "type": ["int", "null"]}, | |
{"name": "favorite_color", "type": ["string", "null"]} | |
] | |
}''' | |
schema = avro.schema.parse(schema_str) | |
writer = avro.io.DatumWriter(schema) | |
bytes_writer = io.BytesIO() | |
encoder = avro.io.BinaryEncoder(bytes_writer) | |
msg = {"name": 'oreh', "favorite_color": 'blue', "favorite_number": random.randint(0,10)} | |
print 'input: %s' % json.dumps(msg) | |
writer.write(msg, encoder) | |
raw_bytes = bytes_writer.getvalue() | |
bytes_reader = io.BytesIO(raw_bytes) | |
decoder = avro.io.BinaryDecoder(bytes_reader) | |
reader = avro.io.DatumReader(schema) | |
rcv_msg = reader.read(decoder) | |
print 'received: %s' % json.dumps(rcv_msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment