Module: LogStash::PluginMixins::Jdbc

Included in:
Filters::Jdbc
Defined in:
lib/logstash/plugin_mixins/jdbc.rb

Overview

Tentative of abstracting JDBC logic to a mixin for potential reuse in other plugins (input/output)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is called when someone includes this module



9
10
11
12
13
# File 'lib/logstash/plugin_mixins/jdbc.rb', line 9

def self.included(base)
  # Add these methods to the 'base' given.
  base.extend(self)
  base.setup_jdbc_config
end

Instance Method Details

#execute_statement(statement, parameters) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/logstash/plugin_mixins/jdbc.rb', line 63

def execute_statement(statement, parameters)
  success = false
  begin 
    #Symbolize parameters keys to use with Sequel
    parameters = parameters.inject({}) do |hash,(k,v)| 
      case v
      when LogStash::Timestamp
        hash[k.to_sym] = v.time
      else
        hash[k.to_sym] = v
      end
      hash
    end
    query = @database[statement,parameters]
    @logger.debug? and @logger.debug("Executing JDBC query", :statement => statement, :parameters => parameters)
    query.all do |row|
      #Stringify row keys
      yield row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash}
    end
    success = true
  rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError => e
    @logger.warn("Exception when executing JDBC query", :exception => e)
  end
  return success
end

#prepare_jdbc_connectionObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/logstash/plugin_mixins/jdbc.rb', line 43

def prepare_jdbc_connection
  require "sequel"
  require "sequel/adapters/jdbc"
  require "java"
  require @jdbc_driver_library if @jdbc_driver_library
  Sequel::JDBC.load_driver(@jdbc_driver_class)
  @database = Sequel.connect(@jdbc_connection_string, :user=> @jdbc_user, :password=>  @jdbc_password.nil? ? nil : @jdbc_password.value)
  if @jdbc_validate_connection
    @database.extension(:connection_validator)
    @database.pool.connection_validation_timeout = @jdcb_validation_timeout
  end
  begin
    @database.test_connection
  rescue Sequel::DatabaseConnectionError => e
    #TODO return false and let the plugin raise a LogStash::ConfigurationError
    raise e
  end
end

#setup_jdbc_configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/logstash/plugin_mixins/jdbc.rb', line 17

def setup_jdbc_config
  # JDBC driver library path to third party driver library.
  config :jdbc_driver_library, :validate => :path

  # JDBC driver class to load, for example "oracle.jdbc.OracleDriver" or "org.apache.derby.jdbc.ClientDriver"
  config :jdbc_driver_class, :validate => :string, :required => true

  # JDBC connection string
  config :jdbc_connection_string, :validate => :string, :required => true

  # JDBC user
  config :jdbc_user, :validate => :string

  # JDBC password
  config :jdbc_password, :validate => :password

  # Connection pool configuration.
  # Validate connection before use.
  config :jdbc_validate_connection, :validate => :boolean, :default => false

  # Connection pool configuration.
  # How often to validate a connection (in seconds)
  config :jdcb_validation_timeout, :validate => :number, :default => 3600
end