Class: Fluent::Plugin::MysqlSelectInsertOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_mysql_select_insert.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/plugin/out_mysql_select_insert.rb', line 55

def configure(conf)
  super

  if select_query =~ /\bwhere\b/i
    fail Fluent::ConfigError, "You can't specify WHERE clause in 'select_query'"
  end
  if select_query !~ /\A\s*select\b/i
    fail Fluent::ConfigError, "'select_query' should begin with 'SELECT'"
  end
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/fluent/plugin/out_mysql_select_insert.rb', line 98

def multi_workers_ready?
  true
end

#write(chunk) ⇒ Object



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
91
92
93
94
95
96
# File 'lib/fluent/plugin/out_mysql_select_insert.rb', line 66

def write(chunk)
  client = new_client
  condition_values = []
  chunk.msgpack_each do |time, record|
    condition_values << "'#{client.escape(record[condition_key])}'"
  end

  sql = <<~SQL
    INSERT #{"IGNORE" if ignore} INTO `#{table}` #{"(#{inserted_columns.join(",")})" if inserted_columns}
    #{select_query}
    WHERE #{condition_column} IN (#{condition_values.join(",")})
  SQL
  sql << " AND (#{extra_condition[0]})" unless extra_condition.empty?

  bound_params = extra_condition[1..-1]&.map { |c| extract_placeholders(c, chunk.) }
  begin
    if bound_params.nil? || bound_params.empty?
      client.query(sql)
    else
      require "mysql2-cs-bind"
      client.xquery(sql, bound_params)
    end
  rescue Mysql2::Error => e
    if e.message.start_with?("Column count doesn't match value count")
      raise Fluent::UnrecoverableError, "#{e.class}: #{e}"
    end
    raise
  end

  client.close
end