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
@@ -205,11 +209,15 @@ def register
205
209
require "rufus/scheduler"
206
210
prepare_jdbc_connection
207
211
208
- # Raise an error if @use_column_value is true, but no @tracking_column is set
209
212
if @use_column_value
213
+ # Raise an error if @use_column_value is true, but no @tracking_column is set
210
214
if @tracking_column . nil?
211
215
raise ( LogStash ::ConfigurationError , "Must set :tracking_column if :use_column_value is true." )
212
216
end
217
+ # Raise an error if @use_column_value is true, and @statements_directory is set
218
+ if @statements_directory
219
+ raise ( LogStash ::ConfigurationError , ":statements_directory must not be set if :use_column_value is true." )
220
+ end
213
221
end
214
222
215
223
@enable_encoding = !@charset . nil? || !@columns_charset . empty?
@@ -221,12 +229,24 @@ def register
221
229
@sql_last_value = YAML . load ( File . read ( @last_run_metadata_path ) )
222
230
end
223
231
224
- unless @statement . nil? ^ @statement_filepath . nil?
232
+ if @statement && @statement_filepath
225
233
raise ( LogStash ::ConfigurationError , "Must set either :statement or :statement_filepath. Only one may be set at a time." )
226
234
end
227
235
228
236
@statement = File . read ( @statement_filepath ) if @statement_filepath
229
237
238
+ unless @statement . nil? ^ @statements_directory . nil?
239
+ raise ( LogStash ::ConfigurationError , "Must set either :statement, :statement_filepath or :statements_directory. Only one may be set at a time" )
240
+ end
241
+
242
+ @statements = [ ]
243
+ if @statements_directory
244
+ Dir . foreach ( @statements_directory ) do |file |
245
+ next if File . directory? file
246
+ @statements . push ( File . read ( @statements_directory + '/' + file ) )
247
+ end
248
+ end
249
+
230
250
if ( @jdbc_password_filepath and @jdbc_password )
231
251
raise ( LogStash ::ConfigurationError , "Only one of :jdbc_password, :jdbc_password_filepath may be set at a time." )
232
252
end
@@ -246,13 +266,25 @@ def run(queue)
246
266
if @schedule
247
267
@scheduler = Rufus ::Scheduler . new ( :max_work_threads => 1 )
248
268
@scheduler . cron @schedule do
249
- execute_query ( queue )
269
+ if @statements . any?
270
+ for statement in @statements
271
+ execute_query ( queue , statement )
272
+ end
273
+ else
274
+ execute_query ( queue , @statement )
275
+ end
250
276
update_state_file
251
277
end
252
278
253
279
@scheduler . join
254
280
else
255
- execute_query ( queue )
281
+ if @statements . any?
282
+ for statement in @statements
283
+ execute_query ( queue , statement )
284
+ end
285
+ else
286
+ execute_query ( queue , @statement )
287
+ end
256
288
update_state_file
257
289
end
258
290
end # def run
@@ -265,10 +297,10 @@ def stop
265
297
266
298
private
267
299
268
- def execute_query ( queue )
300
+ def execute_query ( queue , statement )
269
301
# update default parameters
270
302
@parameters [ 'sql_last_value' ] = @sql_last_value
271
- execute_statement ( @ statement, @parameters ) do |row |
303
+ execute_statement ( statement , @parameters ) do |row |
272
304
if enable_encoding?
273
305
## do the necessary conversions to string elements
274
306
row = Hash [ row . map { |k , v | [ k . to_s , convert ( k , v ) ] } ]
0 commit comments