Class: Fluent::MysqlRecordOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
SetTagKeyMixin, SetTimeKeyMixin
Defined in:
lib/fluent/plugin/out_mysqlrecord.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMysqlRecordOutput

Returns a new instance of MysqlRecordOutput.



19
20
21
22
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 19

def initialize
  super
  require 'mysql2-cs-bind'
end

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



17
18
19
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 17

def handler
  @handler
end

Instance Method Details

#clientObject



40
41
42
43
44
45
46
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 40

def client
  Mysql2::Client.new({
      :host => @host, :port => @port,
      :username => @username, :password => @password,
      :database => @database, :flags => Mysql2::Client::MULTI_STATEMENTS,
    })
end

#configure(conf) ⇒ Object



24
25
26
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 24

def configure(conf)
  super
end

#format(tag, time, record) ⇒ Object



36
37
38
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 36

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#shutdownObject



32
33
34
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 32

def shutdown
  super
end

#startObject



28
29
30
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 28

def start
  super
end

#write(chunk) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin/out_mysqlrecord.rb', line 48

def write(chunk)
  handler = self.client
  chunk.msgpack_each { |tag, time, data|
    cols = data.keys.join(",")
    placeholders = data.map{|k| '?'}.join(',')
    sql = "INSERT INTO #{@table} (#{cols}) VALUES (#{placeholders})"
    handler.xquery(sql, data.values)
  }
  handler.close
end