Class: Chicago::ETL::MysqlFileSink

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

Instance Attribute Summary collapse

Attributes inherited from PipelineEndpoint

#fields

Instance Method Summary collapse

Methods inherited from Sink

#constant_values, #open, #set_constant_values

Methods inherited from PipelineEndpoint

#has_defined_fields?

Constructor Details

#initialize(db, table_name, fields, options = {}) ⇒ MysqlFileSink

Returns a new instance of MysqlFileSink.



14
15
16
17
18
19
20
21
# File 'lib/chicago/etl/mysql_file_sink.rb', line 14

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)



11
12
13
# File 'lib/chicago/etl/mysql_file_sink.rb', line 11

def filepath
  @filepath
end

#truncation_strategy=(value) ⇒ Object (writeonly)



12
13
14
# File 'lib/chicago/etl/mysql_file_sink.rb', line 12

def truncation_strategy=(value)
  @truncation_strategy = value
end

Instance Method Details

#<<(row) ⇒ Object



27
28
29
# File 'lib/chicago/etl/mysql_file_sink.rb', line 27

def <<(row)
  csv << fields.map {|c| @serializer.serialize(row[c]) }
end

#closeObject



31
32
33
34
35
36
# File 'lib/chicago/etl/mysql_file_sink.rb', line 31

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.



40
41
42
43
# File 'lib/chicago/etl/mysql_file_sink.rb', line 40

def load_from_file(file)
  return unless File.size?(file)
  dataset.load_csv_infile(file, @fields, :set => constant_values)
end

#nameObject



23
24
25
# File 'lib/chicago/etl/mysql_file_sink.rb', line 23

def name
  @table_name
end

#truncateObject



45
46
47
48
49
50
51
# File 'lib/chicago/etl/mysql_file_sink.rb', line 45

def truncate
  if @truncation_strategy
    @truncation_strategy.call
  else
    @db[@table_name].truncate
  end
end