logstash-output-jdbc/lib/logstash/outputs/jdbc.rb

58 lines
1.5 KiB
Ruby
Raw Normal View History

2014-04-15 11:32:41 +00:00
# encoding: utf-8
require "logstash/outputs/base"
require "logstash/namespace"
class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
config_name "jdbc"
milestone 1
# Driver class
config :driver_class, :validate => :string
# connection string
config :connection_string, :validate => :string, :required => true
# [ "insert into table (message) values(?)", "%{message}" ]
config :statement, :validate => :array, :required => true
public
def register
@logger.info("Starting up JDBC")
2014-04-16 15:41:45 +00:00
require "java"
2014-04-15 11:32:41 +00:00
jarpath = File.join(File.dirname(__FILE__), "../../../vendor/jar/jdbc/*.jar")
@logger.info(jarpath)
Dir[jarpath].each do |jar|
@logger.debug("JDBC loaded jar", :jar => jar)
require jar
end
import @driver_class
driver = Object.const_get(@driver_class[@driver_class.rindex('.') + 1, @driver_class.length]).new
@connection = driver.connect(@connection_string, java.util.Properties.new)
2014-04-16 15:41:45 +00:00
@logger.debug("JDBC", :driver => driver, :connection => @connection)
2014-04-15 11:32:41 +00:00
end
def receive(event)
return unless output?(event)
2014-04-16 15:41:45 +00:00
return unless @statement.length > 0
2014-04-15 11:32:41 +00:00
statement = @connection.prepareStatement(@statement[0])
2014-04-16 15:41:45 +00:00
@statement[1..-1].each_with_index { |i, idx| statement.setString(idx + 1, event.sprintf(i)) } if @statement.length > 1
2014-04-15 11:32:41 +00:00
@logger.debug("Sending SQL to server", :event => event, :sql => statement.toString())
2014-04-16 15:41:45 +00:00
2014-04-15 11:32:41 +00:00
statement.executeUpdate()
2014-04-16 15:41:45 +00:00
statement.close()
end
def teardown
@connection.close()
super
2014-04-15 11:32:41 +00:00
end
end # class LogStash::Outputs::jdbc