Class: Chicago::Flow::MysqlFileSink
- Inherits:
-
Sink
- Object
- PipelineEndpoint
- Sink
- Chicago::Flow::MysqlFileSink
- Defined in:
- lib/chicago/flow/mysql_file_sink.rb
Instance Attribute Summary collapse
-
#filepath ⇒ Object
readonly
Returns the value of attribute filepath.
Attributes inherited from PipelineEndpoint
Instance Method Summary collapse
- #<<(row) ⇒ Object
- #close ⇒ Object
-
#initialize(db, table_name, fields, options = {}) ⇒ MysqlFileSink
constructor
A new instance of MysqlFileSink.
-
#load_from_file(file) ⇒ Object
Loads data from the file into the MySQL table via LOAD DATA INFILE, if the file exists and has content.
Methods inherited from Sink
Methods inherited from PipelineEndpoint
Constructor Details
#initialize(db, table_name, fields, options = {}) ⇒ MysqlFileSink
Returns a new instance of MysqlFileSink.
12 13 14 15 16 17 18 19 |
# File 'lib/chicago/flow/mysql_file_sink.rb', line 12 def initialize(db, table_name, fields, = {}) @fields = [fields].flatten @filepath = [:filepath] || temp_file(table_name) @serializer = MysqlFileSerializer.new @db = db @table_name = table_name @insert_ignore = !![:ignore] end |
Instance Attribute Details
#filepath ⇒ Object (readonly)
Returns the value of attribute filepath.
10 11 12 |
# File 'lib/chicago/flow/mysql_file_sink.rb', line 10 def filepath @filepath end |
Instance Method Details
#<<(row) ⇒ Object
21 22 23 |
# File 'lib/chicago/flow/mysql_file_sink.rb', line 21 def <<(row) csv << fields.map {|c| @serializer.serialize(row[c]) } end |
#close ⇒ Object
25 26 27 28 29 30 |
# File 'lib/chicago/flow/mysql_file_sink.rb', line 25 def close csv.flush load_from_file(filepath) csv.close File.unlink(filepath) if File.exists?(filepath) end |
#load_from_file(file) ⇒ Object
Loads data from the file into the MySQL table via LOAD DATA INFILE, if the file exists and has content.
34 35 36 37 |
# File 'lib/chicago/flow/mysql_file_sink.rb', line 34 def load_from_file(file) return unless File.size?(file) dataset.load_csv_infile(file, @fields, :set => constant_values) end |