class NdrImport::File::Avro

This class is an avro file handler that returns a single table.

Private Instance Methods

rows() { |keys| ... } click to toggle source
# File lib/ndr_import/file/avro.rb, line 13
def rows(&block)
  return enum_for(:rows) unless block

  # Create an instance of DatumReader
  reader = ::Avro::IO::DatumReader.new
  # Open @filename in read mode
  file   = ::File.open(@filename, 'rb')
  # Equivalent to DataFileReader instance creation in Java
  dr     = ::Avro::DataFile::Reader.new(file, reader)

  dr.each_with_index do |avro_row, i|
    # Ensure the first row is always the "header"
    yield(avro_row.keys) if i.zero?
    yield(avro_row.values.map(&:to_s))
  end
rescue StandardError => e
  raise("#{SafeFile.basename(@filename)} [#{e.class}: #{e.message}]")
end