Search code examples
rubyserializationavro

Avro serialization in Ruby - how to write to a String/buffer instead of file


I am trying to do avro serialization in Ruby. I have written the JSON schema, however I like to have the serialized data as bytes in Ruby instead of writing to file.

My code is hanging something like:

SCHEMA = {
           "type": "record",
           "name": "User",
           "fields" :
             [
               {"name": "name", "type": "string"},
               {"name": "id", "type": "long"},
               {"name": "city", "type": "string"}
             ]
         }.to_json

schema = Avro::Schema.parse(SCHEMA)
dw = Avro::IO::DatumWriter.new(schema)
buffer = StringIO.new
encoder = Avro::IO::BinaryEncoder.new(buffer)
???

I have the values for name, id and city and wondering how to create the User object and serialize it into a string / byte buffer.


Solution

  • Figured out myself:

    schema = Avro::Schema.parse(SCHEMA)
    dw = Avro::IO::DatumWriter.new(schema)
    buffer = StringIO.new
    encoder = Avro::IO::BinaryEncoder.new(buffer)
    datum = Hash["name" =>name, "id" => id, "city" => city]
    dw.write(datum, encoder)
    

    buffer has the resultant avro serialized bytes.

    Looks like there is no Avro client code generation in Ruby yet. We have to pass the data as Ruby hashes like I have done in for datum field.