Class: LogStash::Filters::Jdbc
- Inherits:
-
Base
- Object
- Base
- LogStash::Filters::Jdbc
- Includes:
- PluginMixins::Jdbc
- Defined in:
- lib/logstash/filters/jdbc.rb
Overview
This filter executes a SQL query and store the result set in the field specified as ‘target`.
For example you can load a row based on an id from in the event
- source,ruby
-
filter
jdbc { jdbc_driver_library => "/path/to/mysql-connector-java-5.1.34-bin.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_connection_string => ""jdbc:mysql://localhost:3306/mydatabase" jdbc_user => "me" jdbc_password => "secret" statement => "select * from WORLD.COUNTRY WHERE Code = :code" parameters => { "code" => "country_code" target => "country_details" }
}
Instance Method Summary collapse
Instance Method Details
#filter(event) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/logstash/filters/jdbc.rb', line 50 def filter(event) result = [] #Prepare parameters from event values params = @parameters.inject({}) {|hash,(k,v)| hash[k] = event[event.sprintf(v)] ; hash } #Execute statement and collect results success = execute_statement(@statement,params) do |row| result << row end if success event[@target] = result filter_matched(event) else @tag_on_failure.each do |tag| event["tags"] ||= [] event["tags"] << tag unless event["tags"].include?(tag) end end end |
#register ⇒ Object
45 46 47 |
# File 'lib/logstash/filters/jdbc.rb', line 45 def register prepare_jdbc_connection() end |