Class: Daru::IO::Exporters::Avro
- Defined in:
- lib/daru/io/exporters/avro.rb
Overview
Avro Exporter Class, that extends to_avro_string
and write_avro
methods to
Daru::DataFrame
instance variables
Instance Method Summary collapse
-
#initialize(dataframe, schema = nil) ⇒ Object
constructor
Initializes an Avro Exporter instance.
-
#to_s ⇒ String
Exports an Avro Exporter instance to a file-writable String.
-
#write(path) ⇒ Object
Exports an Avro Exporter instance to an avro file.
Methods inherited from Base
Constructor Details
#initialize(dataframe, schema = nil) ⇒ Object
Initializes an Avro Exporter instance.
46 47 48 49 50 51 52 |
# File 'lib/daru/io/exporters/avro.rb', line 46 def initialize(dataframe, schema=nil) optional_gem 'avro' require 'json' super(dataframe) @schema = schema end |
Instance Method Details
#to_s ⇒ String
Exports an Avro Exporter instance to a file-writable String.
62 63 64 |
# File 'lib/daru/io/exporters/avro.rb', line 62 def to_s super end |
#write(path) ⇒ Object
Exports an Avro Exporter instance to an avro file.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/daru/io/exporters/avro.rb', line 72 def write(path) @schema_obj = process_schema @writer = ::Avro::IO::DatumWriter.new(@schema_obj) @buffer = StringIO.new @writer = ::Avro::DataFile::Writer.new(@buffer, @writer, @schema_obj) @dataframe.each_row { |row| @writer << row.to_h } @writer.close File.open(path, 'w') { |file| file.write(@buffer.string) } end |