class Fluent::MysqlLoadOutput

Constants

QUERY_TEMPLATE

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mysql_load.rb, line 12
def initialize
  require 'mysql2'
  require 'tempfile'
  super
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mysql_load.rb, line 44
def shutdown
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mysql_load.rb, line 40
def start
  super
end
write(chunk) click to toggle source
# 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

Private Instance Methods

get_connection() click to toggle source
# File lib/fluent/plugin/out_mysql_load.rb, line 73
def get_connection
    Mysql2::Client.new({
        :host => @host,
        :port => @port,
        :username => @username,
        :password => @password,
        :database => @database,
        :encoding => @encoding,
        :local_infile => true
      })
end