Class: Fluent::MysqlOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMysqlOutput

Returns a new instance of MysqlOutput.



22
23
24
25
26
# File 'lib/fluent/plugin/out_mysql.rb', line 22

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

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



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

def handler
  @handler
end

Instance Method Details

#clientObject



96
97
98
99
100
101
102
# File 'lib/fluent/plugin/out_mysql.rb', line 96

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

#configure(conf) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/plugin/out_mysql.rb', line 33

def configure(conf)
  super

  # TODO tag_mapped

  case @format
  when 'json'
    @format_proc = Proc.new{|tag, time, record| record.to_json}
  when 'jsonpath'
    @key_names = @key_names.split(/\s*,\s*/)
    @format_proc = Proc.new do |tag, time, record|
      json = record.to_json
      @key_names.map do |k|
        JsonPath.new(k.strip).on(json).first
      end
    end
  else
    @key_names = @key_names.split(/\s*,\s*/)
    @format_proc = Proc.new{|tag, time, record| @key_names.map{|k| record[k]}}
  end

  if @columns.nil? and @sql.nil?
    raise Fluent::ConfigError, "columns or sql MUST be specified, but missing"
  end
  if @columns and @sql
    raise Fluent::ConfigError, "both of columns and sql are specified, but specify one of them"
  end

  if @sql
    begin
      if @format == 'json'
        Mysql2::Client.pseudo_bind(@sql, [nil])
      else
        Mysql2::Client.pseudo_bind(@sql, @key_names.map{|n| nil})
      end
    rescue ArgumentError => e
      raise Fluent::ConfigError, "mismatch between sql placeholders and key_names"
    end
  else # columns
    raise Fluent::ConfigError, "table missing" unless @table
    @columns = @columns.split(/\s*,\s*/)
    cols = @columns.join(',')
    placeholders = if @format == 'json'
                     '?'
                   else
                     @key_names.map{|k| '?'}.join(',')
                   end
    @sql = "INSERT INTO #{@table} (#{cols}) VALUES (#{placeholders})"
  end
end

#format(tag, time, record) ⇒ Object



92
93
94
# File 'lib/fluent/plugin/out_mysql.rb', line 92

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

#shutdownObject



88
89
90
# File 'lib/fluent/plugin/out_mysql.rb', line 88

def shutdown
  super
end

#startObject



84
85
86
# File 'lib/fluent/plugin/out_mysql.rb', line 84

def start
  super
end

#write(chunk) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/fluent/plugin/out_mysql.rb', line 104

def write(chunk)
  handler = self.client
  chunk.msgpack_each { |tag, time, data|
    handler.xquery(@sql, data)
  }
  handler.close
end