86
86
#
87
87
# ==== Configuring multiple SQL statements
88
88
#
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).
95
96
#
96
97
# Please note that if any of the statements use the `sql_last_value` parameter (e.g. for
97
98
# ingesting only data changed since last run), each input should define its own
@@ -147,6 +148,9 @@ class LogStash::Inputs::Jdbc < LogStash::Inputs::Base
147
148
# Path of file containing statement to execute
148
149
config :statement_filepath , :validate => :path
149
150
151
+ # Directory containing statement files to execute
152
+ config :statements_directory , :validate => :path
153
+
150
154
# Hash of query parameter, for example `{ "target_id" => "321" }`
151
155
config :parameters , :validate => :hash , :default => { }
152
156
@@ -206,11 +210,15 @@ def register
206
210
require "rufus/scheduler"
207
211
prepare_jdbc_connection
208
212
209
- # Raise an error if @use_column_value is true, but no @tracking_column is set
210
213
if @use_column_value
214
+ # Raise an error if @use_column_value is true, but no @tracking_column is set
211
215
if @tracking_column . nil?
212
216
raise ( LogStash ::ConfigurationError , "Must set :tracking_column if :use_column_value is true." )
213
217
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
214
222
end
215
223
216
224
@enable_encoding = !@charset . nil? || !@columns_charset . empty?
@@ -222,12 +230,24 @@ def register
222
230
@sql_last_value = YAML . load ( File . read ( @last_run_metadata_path ) )
223
231
end
224
232
225
- unless @statement . nil? ^ @statement_filepath . nil?
233
+ if @statement && @statement_filepath
226
234
raise ( LogStash ::ConfigurationError , "Must set either :statement or :statement_filepath. Only one may be set at a time." )
227
235
end
228
236
229
237
@statement = File . read ( @statement_filepath ) if @statement_filepath
230
238
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
+
231
251
if ( @jdbc_password_filepath and @jdbc_password )
232
252
raise ( LogStash ::ConfigurationError , "Only one of :jdbc_password, :jdbc_password_filepath may be set at a time." )
233
253
end
@@ -247,13 +267,25 @@ def run(queue)
247
267
if @schedule
248
268
@scheduler = Rufus ::Scheduler . new ( :max_work_threads => 1 )
249
269
@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
251
277
update_state_file
252
278
end
253
279
254
280
@scheduler . join
255
281
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
257
289
update_state_file
258
290
end
259
291
end # def run
@@ -266,10 +298,10 @@ def stop
266
298
267
299
private
268
300
269
- def execute_query ( queue )
301
+ def execute_query ( queue , statement )
270
302
# update default parameters
271
303
@parameters [ 'sql_last_value' ] = @sql_last_value
272
- execute_statement ( @ statement, @parameters ) do |row |
304
+ execute_statement ( statement , @parameters ) do |row |
273
305
if enable_encoding?
274
306
## do the necessary conversions to string elements
275
307
row = Hash [ row . map { |k , v | [ k . to_s , convert ( k , v ) ] } ]
0 commit comments