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.
- #multi_workers_ready? ⇒ Boolean
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ MysqlBulkOutput
Returns a new instance of MysqlBulkOutput.
65 66 67 68 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 65 def initialize super require 'mysql2-cs-bind' end |
Instance Attribute Details
#handler ⇒ Object
Returns the value of attribute handler.
63 64 65 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 63 def handler @handler end |
Instance Method Details
#check_table_schema(database: @database, table: @table) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 115 def check_table_schema(database: @database, table: @table) _client = client(database) result = _client.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 ensure if not _client.nil? then _client.close end end |
#client(database) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 147 def client(database) 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 70 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, <<-DESC on_duplicate_update_keys and on_duplicate_update_custom_values must be the same length DESC 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) @values_template = "(#{ @column_names.map { |key| '?' }.join(',') })" @insert_columns = @column_names.map{|x| "`#{x.to_s.gsub('`', '``')}`"}.join(',') @json_key_names = @json_key_names.split(',') if @json_key_names @unixtimestamp_key_names = @unixtimestamp_key_names.split(',') if @unixtimestamp_key_names end |
#expand_placeholders(metadata) ⇒ Object
164 165 166 167 168 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 164 def () database = extract_placeholders(@database, ).gsub('.', '_') table = extract_placeholders(@table, ).gsub('.', '_') return database, table end |
#format(tag, time, record) ⇒ Object
134 135 136 137 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 134 def format(tag, time, record) record = inject_values_to_record(tag, time, record) [tag, time, record].to_msgpack end |
#formatted_to_msgpack_binary ⇒ Object
139 140 141 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 139 def formatted_to_msgpack_binary true end |
#multi_workers_ready? ⇒ Boolean
143 144 145 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 143 def multi_workers_ready? true end |
#write(chunk) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/fluent/plugin/out_mysql_bulk.rb', line 170 def write(chunk) database, table = (chunk.) max_lengths = check_table_schema(database: database, table: table) @handler = client(database) values = [] 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 @handler.query("SET SESSION TRANSACTION ISOLATION LEVEL #{transaction_isolation_level}") if @transaction_isolation_level slice_size = @max_rows_per_insert > 0 ? @max_rows_per_insert : values.length values.each_slice(slice_size) do |slice| sql = "INSERT #{@insert_ignore ? "IGNORE" : ""} INTO #{table} (#{@insert_columns}) VALUES #{values.join(',')}" sql += @on_duplicate_key_update_sql if @on_duplicate_key_update @handler.xquery(sql) end log.info "bulk insert values size (table: #{@table}) => #{values.size}" @handler.close end |