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.


28
29
30
31
32
# File 'lib/fluent/plugin/out_mysql.rb', line 28

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

Instance Attribute Details

#handlerObject

Returns the value of attribute handler


26
27
28
# File 'lib/fluent/plugin/out_mysql.rb', line 26

def handler
  @handler
end

Instance Method Details

#clientObject


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fluent/plugin/out_mysql.rb', line 104

def client
  Mysql2::Client.new({
      :host => @host, :port => @port,
      :username => @username, :password => @password,
      :database => @database,
      :sslkey => @sslkey,
      :sslcert => @sslcert,
      :sslca => @sslca,
      :sslcapath => @sslcapath,
      :sslcipher => @sslcipher,
      :sslverify => @sslverify,
      :flags => Mysql2::Client::MULTI_STATEMENTS,
    })
end

#configure(conf) ⇒ Object


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
83
84
85
86
87
88
89
90
# File 'lib/fluent/plugin/out_mysql.rb', line 39

def configure(conf)
  super

  log.warn "[mysql] This plugin deprecated. You should use mysql_bulk."

  # 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


100
101
102
# File 'lib/fluent/plugin/out_mysql.rb', line 100

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

#shutdownObject


96
97
98
# File 'lib/fluent/plugin/out_mysql.rb', line 96

def shutdown
  super
end

#startObject


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

def start
  super
end

#write(chunk) ⇒ Object


119
120
121
122
123
124
125
# File 'lib/fluent/plugin/out_mysql.rb', line 119

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