-
-
Save amir20/4048488 to your computer and use it in GitHub Desktop.
Avro IO with different reader and writer schemas
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
WRITER_SCHEMA = <<-JSON | |
{ "type": "record", | |
"name": "User", | |
"fields" : [ | |
{"name": "username", "type": "string"}, | |
{"name": "age", "type": "int"}, | |
{"name": "verified", "type": "boolean", "default": "false"} | |
]} | |
JSON | |
READER_SCHEMA = <<-JSON | |
{ "type": "record", | |
"name": "User", | |
"fields" : [ | |
{"name": "username", "type": "string"}, | |
{"name": "verified", "type": "boolean", "default": "false"} | |
]} | |
JSON | |
# write a file | |
file = File.open('raw.avro', 'w') | |
encoder = Avro::IO::BinaryEncoder.new(file) | |
writer_schema = Avro::Schema.parse(WRITER_SCHEMA) | |
writer = Avro::IO::DatumWriter.new(writer_schema) | |
writer.write({"username" => "tom", "age" => 25, "verified" => true }, encoder) | |
writer.write({"username" => "dick", "age" => 23, "verified" => false }, encoder) | |
writer.write({"username" => "harry", "age" => 34, "verified" => false }, encoder) | |
file.flush | |
file.close | |
# read the file | |
file = File.open('raw.avro', 'r') | |
decoder = Avro::IO::BinaryDecoder.new(file) | |
reader_schema = Avro::Schema.parse(READER_SCHEMA) | |
reader = Avro::IO::DatumReader.new(writer_schema, reader_schema) | |
p reader.read(decoder) | |
p reader.read(decoder) | |
p reader.read(decoder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment