8686#
8787# ==== Configuring multiple SQL statements
8888#
89- # Configuring multiple SQL statements is useful when there is a need to query and ingest data
90- # from different database tables or views. It is possible to define separate Logstash
91- # configuration files for each statement or to define multiple statements in a single configuration
92- # file. When using multiple statements in a single Logstash configuration file, each statement
93- # has to be defined as a separate jdbc input (including jdbc driver, connection string and other
94- # required parameters).
89+ # Configuring multiple SQL statements is useful when there is a need to query and ingest data
90+ # from different database tables or views. It is possible to define separate Logstash
91+ # configuration files for each statement, to define multiple statements in a single configuration
92+ # file or to use the `statements_directory` parameter where all statements within the configured
93+ # directory get executed. When using multiple statements in a single Logstash configuration file,
94+ # each statement has to be defined as a separate jdbc input (including jdbc driver, connection
95+ # string and other required parameters).
9596#
9697# Please note that if any of the statements use the `sql_last_value` parameter (e.g. for
9798# ingesting only data changed since last run), each input should define its own
@@ -147,6 +148,9 @@ class LogStash::Inputs::Jdbc < LogStash::Inputs::Base
147148 # Path of file containing statement to execute
148149 config :statement_filepath , :validate => :path
149150
151+ # Directory containing statement files to execute
152+ config :statements_directory , :validate => :path
153+
150154 # Hash of query parameter, for example `{ "target_id" => "321" }`
151155 config :parameters , :validate => :hash , :default => { }
152156
@@ -206,11 +210,15 @@ def register
206210 require "rufus/scheduler"
207211 prepare_jdbc_connection
208212
209- # Raise an error if @use_column_value is true, but no @tracking_column is set
210213 if @use_column_value
214+ # Raise an error if @use_column_value is true, but no @tracking_column is set
211215 if @tracking_column . nil?
212216 raise ( LogStash ::ConfigurationError , "Must set :tracking_column if :use_column_value is true." )
213217 end
218+ # Raise an error if @use_column_value is true, and @statements_directory is set
219+ if @statements_directory
220+ raise ( LogStash ::ConfigurationError , ":statements_directory must not be set if :use_column_value is true." )
221+ end
214222 end
215223
216224 @enable_encoding = !@charset . nil? || !@columns_charset . empty?
@@ -222,12 +230,24 @@ def register
222230 @sql_last_value = YAML . load ( File . read ( @last_run_metadata_path ) )
223231 end
224232
225- unless @statement . nil? ^ @statement_filepath . nil?
233+ if @statement && @statement_filepath
226234 raise ( LogStash ::ConfigurationError , "Must set either :statement or :statement_filepath. Only one may be set at a time." )
227235 end
228236
229237 @statement = File . read ( @statement_filepath ) if @statement_filepath
230238
239+ unless @statement . nil? ^ @statements_directory . nil?
240+ raise ( LogStash ::ConfigurationError , "Must set either :statement, :statement_filepath or :statements_directory. Only one may be set at a time" )
241+ end
242+
243+ @statements = [ ]
244+ if @statements_directory
245+ Dir . foreach ( @statements_directory ) do |file |
246+ next if File . directory? file
247+ @statements . push ( File . read ( @statements_directory + '/' + file ) )
248+ end
249+ end
250+
231251 if ( @jdbc_password_filepath and @jdbc_password )
232252 raise ( LogStash ::ConfigurationError , "Only one of :jdbc_password, :jdbc_password_filepath may be set at a time." )
233253 end
@@ -247,13 +267,25 @@ def run(queue)
247267 if @schedule
248268 @scheduler = Rufus ::Scheduler . new ( :max_work_threads => 1 )
249269 @scheduler . cron @schedule do
250- execute_query ( queue )
270+ if @statements . any?
271+ for statement in @statements
272+ execute_query ( queue , statement )
273+ end
274+ else
275+ execute_query ( queue , @statement )
276+ end
251277 update_state_file
252278 end
253279
254280 @scheduler . join
255281 else
256- execute_query ( queue )
282+ if @statements . any?
283+ for statement in @statements
284+ execute_query ( queue , statement )
285+ end
286+ else
287+ execute_query ( queue , @statement )
288+ end
257289 update_state_file
258290 end
259291 end # def run
@@ -266,10 +298,10 @@ def stop
266298
267299 private
268300
269- def execute_query ( queue )
301+ def execute_query ( queue , statement )
270302 # update default parameters
271303 @parameters [ 'sql_last_value' ] = @sql_last_value
272- execute_statement ( @ statement, @parameters ) do |row |
304+ execute_statement ( statement , @parameters ) do |row |
273305 if enable_encoding?
274306 ## do the necessary conversions to string elements
275307 row = Hash [ row . map { |k , v | [ k . to_s , convert ( k , v ) ] } ]
0 commit comments