Class: Fluent::MysqlLoadOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_mysql_load.rb

Constant Summary collapse

QUERY_TEMPLATE =
"LOAD DATA LOCAL INFILE '%s' INTO TABLE %s (%s)"

Instance Method Summary collapse

Constructor Details

#initializeMysqlLoadOutput

Returns a new instance of MysqlLoadOutput.



12
13
14
15
16
# File 'lib/fluent/plugin/out_mysql_load.rb', line 12

def initialize
  require 'mysql2'
  require 'tempfile'
  super
end

Instance Method Details

#configure(conf) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fluent/plugin/out_mysql_load.rb', line 28

def configure(conf)
  super
  if @database.nil? || @tablename.nil? || @column_names.nil?
    raise Fluent::ConfigError, "database and tablename and column_names is required."
  end

  @key_names = @key_names.nil? ? @column_names.split(',') : @key_names.split(',')
  unless @column_names.split(',').count == @key_names.count
    raise Fluent::ConfigError, "It does not take the integrity of the key_names and column_names."
  end
end

#format(tag, time, record) ⇒ Object



48
49
50
51
52
53
# File 'lib/fluent/plugin/out_mysql_load.rb', line 48

def format(tag, time, record)
  values = @key_names.map { |k|
    k == '${time}' ? Time.at(time).strftime('%Y-%m-%d %H:%M:%S') : record[k]
  }
  values.to_msgpack
end

#shutdownObject



44
45
46
# File 'lib/fluent/plugin/out_mysql_load.rb', line 44

def shutdown
  super
end

#startObject



40
41
42
# File 'lib/fluent/plugin/out_mysql_load.rb', line 40

def start
  super
end

#write(chunk) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/out_mysql_load.rb', line 55

def write(chunk)
  data_count = 0
  tmp = Tempfile.new("mysql-loaddata")
  chunk.msgpack_each { |record|
    tmp.write record.join("\t") + "\n"
    data_count += 1
  }
  tmp.close

  conn = get_connection
  conn.query(QUERY_TEMPLATE % ([tmp.path, @tablename, @column_names]))
  conn.close

  log.info "number that is registered in the \"%s:%s\" table is %d" % ([@database, @tablename, data_count])
end