Skip to content

Instantly share code, notes, and snippets.

@lurenx
Created March 24, 2016 09:33
Show Gist options
  • Select an option

  • Save lurenx/3147f6e8b4095505e2cd to your computer and use it in GitHub Desktop.

Select an option

Save lurenx/3147f6e8b4095505e2cd to your computer and use it in GitHub Desktop.
deserialise avro
require 'rubygems'
require 'avro'
# Open items.avro file in read mode
file = File.open('items.avro', 'rb')
# Create an instance of DatumReader
reader = Avro::IO::DatumReader.new()
# Equivalent to DataFileReader instance creation in Java
dr = Avro::DataFile::Reader.new(file, reader)
p schema.class
schema = dr.meta["avro.schema"]
schema = Avro::Schema.parse(schema)
p schema.class
# For each record type in the input file prints the fields mentioned
# in print command on console. Each output field is tab seperated
dr.each {|record|
print record["name"],"\t",record["description"],"\t",record["price"],"\n"
}
# Close the input file
dr.close
dw = Avro::IO::DatumWriter.new(schema)
buffer = StringIO.new
encoder = Avro::IO::BinaryEncoder.new(buffer)
datum = {"name" => "Desktop", "description" => "Office and Personal Usage", "price" => 100}
dw.write(datum, encoder)
p buffer.methods
p buffer.length
p "################"
c=buffer.string
p "################"
p datum.class
p buffer
stringreader = StringIO.new(c)
decoder = Avro::IO::BinaryDecoder.new(stringreader)
datumreader = Avro::IO::DatumReader.new(schema)
#read the message
read_value = datumreader.read(decoder)
p read_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment