Class: Chicago::Flow::MysqlFileSink

Inherits:
Sink show all
Defined in:
lib/chicago/flow/mysql_file_sink.rb

Instance Attribute Summary collapse

Attributes inherited from PipelineEndpoint

#fields

Instance Method Summary collapse

Methods inherited from Sink

#constant_values, #open

Methods inherited from PipelineEndpoint

#has_defined_fields?

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, options = {})
  @fields = [fields].flatten
  @filepath = options[:filepath] || temp_file(table_name)
  @serializer = MysqlFileSerializer.new
  @db = db
  @table_name = table_name
  @insert_ignore = !!options[:ignore]
end

Instance Attribute Details

#filepathObject (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

#closeObject



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