Sha256: 699181f68e35f623f28085bb539e9ad776675fc3c66a951c4b60c1a8a103666f

Contents?: true

Size: 1.1 KB

Versions: 8

Compression:

Stored size: 1.1 KB

Contents

require 'avro'
require 'ndr_support/safe_file'
require_relative 'registry'

module NdrImport
  # This is one of a collection of file handlers that deal with individual formats of data.
  # They can be instantiated directly or via the factory method Registry.tables
  module File
    # This class is an avro file handler that returns a single table.
    class Avro < Base
      private

      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
    end
    Registry.register(Avro, 'avro')
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ndr_import-11.2.1 lib/ndr_import/file/avro.rb
ndr_import-11.2.0 lib/ndr_import/file/avro.rb
ndr_import-11.1.0 lib/ndr_import/file/avro.rb
ndr_import-11.0.2 lib/ndr_import/file/avro.rb
ndr_import-11.0.1 lib/ndr_import/file/avro.rb
ndr_import-11.0.0 lib/ndr_import/file/avro.rb
ndr_import-10.3.0 lib/ndr_import/file/avro.rb
ndr_import-10.2.0 lib/ndr_import/file/avro.rb