Class: Fluent::SolrOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
SetTagKeyMixin, SetTimeKeyMixin
Defined in:
lib/fluent/plugin/out_solr.rb

Constant Summary collapse

DEFAULT_COLLECTION =
'collection1'
DEFAULT_IGNORE_UNDEFINED_FIELDS =
false
DEFAULT_TAG_FIELD =
'tag'
DEFAULT_TIMESTAMP_FIELD =
'event_timestamp'
DEFAULT_FLUSH_SIZE =
100
MODE_STANDALONE =
'Standalone'
MODE_SOLRCLOUD =
'SolrCloud'

Instance Method Summary collapse

Constructor Details

#initializeSolrOutput

Returns a new instance of SolrOutput.



48
49
50
# File 'lib/fluent/plugin/out_solr.rb', line 48

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fluent/plugin/out_solr.rb', line 52

def configure(conf)
  super

  @url = conf['url']

  @zk_host = conf['zk_host']
  @collection = conf.has_key?('collection') ? conf['collection'] : DEFAULT_COLLECTION

  @defined_fields = conf['defined_fields']
  @ignore_undefined_fields = conf.has_key?('ignore_undefined_fields') ? conf['ignore_undefined_fields'] : DEFAULT_IGNORE_UNDEFINED_FIELDS

  @unique_key_field = conf['unique_key_field']
  @tag_field = conf.has_key?('tag_field') ? conf['tag_field'] : DEFAULT_TAG_FIELD
  @timestamp_field = conf.has_key?('timestamp_field') ? conf['timestamp_field'] : DEFAULT_TIMESTAMP_FIELD

  @flush_size = conf.has_key?('flush_size') ? conf['flush_size'].to_i : DEFAULT_FLUSH_SIZE
end

#format(tag, time, record) ⇒ Object



100
101
102
# File 'lib/fluent/plugin/out_solr.rb', line 100

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

#get_fieldsObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fluent/plugin/out_solr.rb', line 172

def get_fields
  response = nil

  if @mode == MODE_STANDALONE then
    response = @solr.get 'schema/fields'
  elsif @mode == MODE_SOLRCLOUD then
    response = @solr.get 'schema/fields', collection: @collection
  end

  fields = []
  response['fields'].each do |field|
    fields.push(field['name'])
  end
  log.info ("Fields: #{fields}")

  return fields

  rescue Exception => e
    log.warn("An error occurred while indexing: #{e.message}")
end

#get_unique_keyObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fluent/plugin/out_solr.rb', line 154

def get_unique_key
  response = nil

  if @mode == MODE_STANDALONE then
    response = @solr.get 'schema/uniquekey'
  elsif @mode == MODE_SOLRCLOUD then
    response = @solr.get 'schema/uniquekey', collection: @collection
  end

  unique_key = response['uniqueKey']
  log.info ("Unique key: #{unique_key}")

  return unique_key

  rescue Exception => e
    log.warn("An error occurred while indexing: #{e.message}")
end

#shutdownObject



92
93
94
95
96
97
98
# File 'lib/fluent/plugin/out_solr.rb', line 92

def shutdown
  super

  unless @zk.nil? then
    @zk.close
  end
end

#startObject



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_solr.rb', line 70

def start
  super

  @mode = nil
  if ! @url.nil? then
    @mode = MODE_STANDALONE
  elsif ! @zk_host.nil?
    @mode = MODE_SOLRCLOUD
  end

  @solr = nil
  @zk = nil

  if @mode == MODE_STANDALONE then
    @solr = RSolr.connect :url => @url
  elsif @mode == MODE_SOLRCLOUD then
    @zk = ZK.new(@zk_host)
    cloud_connection = RSolr::Cloud::Connection.new(@zk)
    @solr = RSolr::Client.new(cloud_connection, read_timeout: 60, open_timeout: 60)
  end
end

#update(documents) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fluent/plugin/out_solr.rb', line 142

def update(documents)
  if @mode == MODE_STANDALONE then
    @solr.add documents, :params => {:commit => true}
    log.info "Added %d document(s) to Solr" % documents.count
  elsif @mode == MODE_SOLRCLOUD then
    @solr.add documents, collection: @collection, :params => {:commit => true}
    log.info "Added %d document(s) to Solr" % documents.count
  end
  rescue Exception => e
    log.warn("An error occurred while indexing: #{e.message}")
end

#write(chunk) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fluent/plugin/out_solr.rb', line 104

def write(chunk)
  documents = []

  @fields = @defined_fields.nil? ? get_fields : @defined_fields
  @unique_key = @unique_key_field.nil? ? get_unique_key : @unique_key_field

  chunk.msgpack_each do |tag, time, record|
    unless record.has_key?(@unique_key) then
      record.merge!({@unique_key => SecureRandom.uuid})
    end

    unless record.has_key?(@tag_field) then
      record.merge!({@tag_field => tag})
    end

    unless record.has_key?(@timestamp_field) then
      record.merge!({@timestamp_field => Time.at(time).utc.strftime('%FT%TZ')})
    end

    if @ignore_undefined_fields then
      record.each_key do |key|
        unless @fields.include?(key) then
          record.delete(key)
        end
      end
    end

    documents << record

    if documents.count >= @flush_size
      update documents
      documents.clear
    end
  end

  update documents unless documents.empty?
end