Skip to content

Instantly share code, notes, and snippets.

@grantr
Created May 27, 2010 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantr/416080 to your computer and use it in GitHub Desktop.
Save grantr/416080 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'avro'
count = ARGV[0] ? ARGV[0].to_i : 10000
puts "writing #{count} records"
SCHEMA = <<-JSON
{ "type": "record",
"name": "User",
"fields" : [
{"name": "username", "type": "string"},
{"name": "age", "type": "int"},
{"name": "verified", "type": "boolean", "default": "false"}
]}
JSON
file = File.open('data.avr', 'wb')
schema = Avro::Schema.parse(SCHEMA)
writer = Avro::IO::DatumWriter.new(schema)
dw = Avro::DataFile::Writer.new(file, writer, schema)
count.times do
dw << {"username" => "john", "age" => 25, "verified" => true}
end
dw.close
puts "data.avr: #{File.new("data.avr").stat.size}"
file = File.open('data.json', 'w')
count.times do
file << Yajl::Encoder.encode({"username" => "john", "age" => 25, "verified" => true})
end
file.close
puts "data.json: #{File.new("data.json").stat.size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment