Class: Fluent::MysqlReplicatorElasticsearchOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_mysql_replicator_elasticsearch.rb

Constant Summary collapse

DEFAULT_TAG_FORMAT =
/(?<index_name>[^\.]+)\.(?<type_name>[^\.]+)\.(?<event>[^\.]+)\.(?<primary_key>[^\.]+)$/

Instance Method Summary collapse

Constructor Details

#initializeMysqlReplicatorElasticsearchOutput

Returns a new instance of MysqlReplicatorElasticsearchOutput.



16
17
18
# File 'lib/fluent/plugin/out_mysql_replicator_elasticsearch.rb', line 16

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/fluent/plugin/out_mysql_replicator_elasticsearch.rb', line 20

def configure(conf)
  super

  if @tag_format.nil? || @tag_format == DEFAULT_TAG_FORMAT
    @tag_format = DEFAULT_TAG_FORMAT
  else
    @tag_format = Regexp.new(conf['tag_format'])
  end
end

#format(tag, time, record) ⇒ Object



34
35
36
# File 'lib/fluent/plugin/out_mysql_replicator_elasticsearch.rb', line 34

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

#shutdownObject



38
39
40
# File 'lib/fluent/plugin/out_mysql_replicator_elasticsearch.rb', line 38

def shutdown
  super
end

#startObject



30
31
32
# File 'lib/fluent/plugin/out_mysql_replicator_elasticsearch.rb', line 30

def start
  super
end

#write(chunk) ⇒ Object



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
# File 'lib/fluent/plugin/out_mysql_replicator_elasticsearch.rb', line 42

def write(chunk)
  bulk_message = []

  chunk.msgpack_each do |tag, time, record|
    tag_parts = tag.match(@tag_format)
    target_index = tag_parts['index_name']
    target_type = tag_parts['type_name']
    id_key = tag_parts['primary_key']

    if tag_parts['event'] == 'delete'
      meta = { "delete" => {"_index" => target_index, "_type" => target_type, "_id" => record[id_key]} }
      bulk_message << Yajl::Encoder.encode(meta)
    else
      meta = { "index" => {"_index" => target_index, "_type" => target_type} }
      if id_key && record[id_key]
        meta['index']['_id'] = record[id_key]
      end
      bulk_message << Yajl::Encoder.encode(meta)
      bulk_message << Yajl::Encoder.encode(record)
    end
  end
  bulk_message << ""

  http = Net::HTTP.new(@host, @port.to_i)
  http.use_ssl = @ssl

  request = Net::HTTP::Post.new('/_bulk', {'content-type' => 'application/json; charset=utf-8'})
  if @username && @password
    request.basic_auth(@username, @password)
  end

  request.body = bulk_message.join("\n")
  http.request(request).value
end