Class: Fluent::MysqlOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::MysqlOutput
- Includes:
- SetTagKeyMixin, SetTimeKeyMixin
- Defined in:
- lib/fluent/plugin/out_mysql.rb
Instance Attribute Summary collapse
-
#handler ⇒ Object
Returns the value of attribute handler.
Instance Method Summary collapse
- #client ⇒ Object
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
-
#initialize ⇒ MysqlOutput
constructor
A new instance of MysqlOutput.
- #shutdown ⇒ Object
- #start ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ MysqlOutput
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
#handler ⇒ Object
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
#client ⇒ Object
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 |
#shutdown ⇒ Object
96 97 98 |
# File 'lib/fluent/plugin/out_mysql.rb', line 96 def shutdown super end |
#start ⇒ Object
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 |