Class: Fluent::Plugin::MysqlBulkOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::Plugin::MysqlBulkOutput
- Defined in:
- lib/fluent/plugin/out_mysql_bulk.rb
Instance Attribute Summary collapse
-
#handler ⇒ Object
Returns the value of attribute handler.
Instance Method Summary collapse
- #check_table_schema(database: @database, table: @table) ⇒ Object
- #client(database) ⇒ Object
- #configure(conf) ⇒ Object
- #expand_placeholders(metadata) ⇒ Object
- #format(tag, time, record) ⇒ Object
- #formatted_to_msgpack_binary ⇒ Object
-
#initialize ⇒ MysqlBulkOutput
constructor
A new instance of MysqlBulkOutput.
- #shutdown ⇒ Object
- #start ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ MysqlBulkOutput
Returns a new instance of MysqlBulkOutput.
41 42 43 44 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 41 def initialize super require 'mysql2-cs-bind' end |
Instance Attribute Details
#handler ⇒ Object
Returns the value of attribute handler.
39 40 41 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 39 def handler @handler end |
Instance Method Details
#check_table_schema(database: @database, table: @table) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 92 def check_table_schema(database: @database, table: @table) result = client(database).xquery("SHOW COLUMNS FROM #{table}") max_lengths = [] @column_names.each do |column| info = result.select { |x| x['Field'] == column }.first r = /(char|varchar)\(([\d]+)\)/ begin max_length = info['Type'].scan(r)[0][1].to_i rescue max_length = nil end max_lengths << max_length end max_lengths end |
#client(database) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 121 def client(database) Mysql2::Client.new( host: @host, port: @port, username: @username, password: @password, database: database, flags: Mysql2::Client::MULTI_STATEMENTS ) end |
#configure(conf) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 46 def configure(conf) compat_parameters_convert(conf, :buffer, :inject) super if @column_names.nil? fail Fluent::ConfigError, 'column_names MUST specified, but missing' end if @on_duplicate_key_update if @on_duplicate_update_keys.nil? fail Fluent::ConfigError, 'on_duplicate_key_update = true , on_duplicate_update_keys nil!' end @on_duplicate_update_keys = @on_duplicate_update_keys.split(',') if !@on_duplicate_update_custom_values.nil? @on_duplicate_update_custom_values = @on_duplicate_update_custom_values.split(',') if @on_duplicate_update_custom_values.length != @on_duplicate_update_keys.length fail Fluent::ConfigError, "on_duplicate_update_keys and on_duplicate_update_custom_values must be the same length\n" end end @on_duplicate_key_update_sql = ' ON DUPLICATE KEY UPDATE ' updates = [] @on_duplicate_update_keys.each_with_index do |update_column, i| if @on_duplicate_update_custom_values.nil? || @on_duplicate_update_custom_values[i] == "#{update_column}" updates << "#{update_column} = VALUES(#{update_column})" else value = @on_duplicate_update_custom_values[i].to_s.match(/\${(.*)}/)[1] escape_value = Mysql2::Client.escape(value) updates << "#{update_column} = #{escape_value}" end end @on_duplicate_key_update_sql += updates.join(',') end @column_names = @column_names.split(',').collect(&:strip) @key_names = @key_names.nil? ? @column_names : @key_names.split(',').collect(&:strip) @json_key_names = @json_key_names.split(',') if @json_key_names end |
#expand_placeholders(metadata) ⇒ Object
132 133 134 135 136 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 132 def () database = extract_placeholders(@database, ).gsub('.', '_') table = extract_placeholders(@table, ).gsub('.', '_') return database, table end |
#format(tag, time, record) ⇒ Object
112 113 114 115 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 112 def format(tag, time, record) record = inject_values_to_record(tag, time, record) [tag, time, record].to_msgpack end |
#formatted_to_msgpack_binary ⇒ Object
117 118 119 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 117 def formatted_to_msgpack_binary true end |
#shutdown ⇒ Object
108 109 110 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 108 def shutdown super end |
#start ⇒ Object
88 89 90 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 88 def start super end |
#write(chunk) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 138 def write(chunk) database, table = (chunk.) max_lengths = check_table_schema(database: database, table: table) @handler = client(database) values = [] values_template = "(#{ @column_names.map { |key| '?' }.join(',') })" chunk.msgpack_each do |tag, time, data| data = format_proc.call(tag, time, data, max_lengths) values << Mysql2::Client.pseudo_bind(values_template, data) end sql = "INSERT INTO #{table} (#{@column_names.join(',')}) VALUES #{values.join(',')}" sql += @on_duplicate_key_update_sql if @on_duplicate_key_update log.info "bulk insert values size (table: #{table}) => #{values.size}" @handler.xquery(sql) @handler.close end |