Class: Daru::IO::Importers::Avro

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/importers/avro.rb

Overview

Avro Importer Class, that extends read_avro method to Daru::DataFrame

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initializeAvro

Note:

The 'snappy' gem handles compressions and is used within Avro gem. Yet, it isn't specified as a dependency in Avro gem. Hence, it has been added separately.

Checks for required gem dependencies of Avro Importer



14
15
16
17
# File 'lib/daru/io/importers/avro.rb', line 14

def initialize
  optional_gem 'avro'
  optional_gem 'snappy'
end

Class Method Details

.read(path) ⇒ Daru::IO::Importers::Avro

Reads data from an avro file

Examples:

Reading from avro file

instance = Daru::IO::Importers::Avro.read("azorahai.avro")

Parameters:

  • path (String)

    Path to Avro file, where the dataframe is to be imported from.

Returns:



29
30
31
32
33
34
# File 'lib/daru/io/importers/avro.rb', line 29

def read(path)
  @path   = path
  @buffer = StringIO.new(File.read(@path))
  @data = ::Avro::DataFile::Reader.new(@buffer, ::Avro::IO::DatumReader.new).to_a
  self
end

Instance Method Details

#callDaru::DataFrame

Imports a Daru::DataFrame from an Avro Importer instance

Examples:

Importing from an Avro file

df = instance.call

#=> #<Daru::DataFrame(3x3)>
#        name points winner
#    0   Dany    100   true
#    1    Jon    100   true
#    2 Tyrion    100   true

Returns:



48
49
50
# File 'lib/daru/io/importers/avro.rb', line 48

def call
  Daru::DataFrame.new(@data)
end

#read(path) ⇒ Daru::IO::Importers::Avro

Reads data from an avro file

Examples:

Reading from avro file

instance = Daru::IO::Importers::Avro.read("azorahai.avro")

Parameters:

  • path (String)

    Path to Avro file, where the dataframe is to be imported from.

Returns:



29
30
31
32
33
34
# File 'lib/daru/io/importers/avro.rb', line 29

def read(path)
  @path   = path
  @buffer = StringIO.new(File.read(@path))
  @data = ::Avro::DataFile::Reader.new(@buffer, ::Avro::IO::DatumReader.new).to_a
  self
end