logstash-integration-jdbc 5.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/CONTRIBUTORS +22 -0
  4. data/Gemfile +11 -0
  5. data/LICENSE +13 -0
  6. data/NOTICE.TXT +5 -0
  7. data/README.md +105 -0
  8. data/docs/filter-jdbc_static.asciidoc +606 -0
  9. data/docs/filter-jdbc_streaming.asciidoc +317 -0
  10. data/docs/index.asciidoc +32 -0
  11. data/docs/input-jdbc.asciidoc +573 -0
  12. data/lib/logstash/filters/jdbc/basic_database.rb +125 -0
  13. data/lib/logstash/filters/jdbc/column.rb +39 -0
  14. data/lib/logstash/filters/jdbc/db_object.rb +101 -0
  15. data/lib/logstash/filters/jdbc/loader.rb +119 -0
  16. data/lib/logstash/filters/jdbc/loader_schedule.rb +64 -0
  17. data/lib/logstash/filters/jdbc/lookup.rb +253 -0
  18. data/lib/logstash/filters/jdbc/lookup_processor.rb +100 -0
  19. data/lib/logstash/filters/jdbc/lookup_result.rb +40 -0
  20. data/lib/logstash/filters/jdbc/read_only_database.rb +57 -0
  21. data/lib/logstash/filters/jdbc/read_write_database.rb +108 -0
  22. data/lib/logstash/filters/jdbc/repeating_load_runner.rb +13 -0
  23. data/lib/logstash/filters/jdbc/single_load_runner.rb +46 -0
  24. data/lib/logstash/filters/jdbc/validatable.rb +46 -0
  25. data/lib/logstash/filters/jdbc_static.rb +240 -0
  26. data/lib/logstash/filters/jdbc_streaming.rb +196 -0
  27. data/lib/logstash/inputs/jdbc.rb +341 -0
  28. data/lib/logstash/inputs/tzinfo_jruby_patch.rb +57 -0
  29. data/lib/logstash/plugin_mixins/jdbc/checked_count_logger.rb +43 -0
  30. data/lib/logstash/plugin_mixins/jdbc/jdbc.rb +298 -0
  31. data/lib/logstash/plugin_mixins/jdbc/statement_handler.rb +129 -0
  32. data/lib/logstash/plugin_mixins/jdbc/value_tracking.rb +140 -0
  33. data/lib/logstash/plugin_mixins/jdbc_streaming/cache_payload.rb +28 -0
  34. data/lib/logstash/plugin_mixins/jdbc_streaming/parameter_handler.rb +64 -0
  35. data/lib/logstash/plugin_mixins/jdbc_streaming/statement_handler.rb +143 -0
  36. data/lib/logstash/plugin_mixins/jdbc_streaming.rb +100 -0
  37. data/lib/logstash/plugin_mixins/statement_handler.rb +0 -0
  38. data/lib/logstash-integration-jdbc_jars.rb +5 -0
  39. data/logstash-integration-jdbc.gemspec +44 -0
  40. data/spec/filters/env_helper.rb +10 -0
  41. data/spec/filters/integration/jdbc_static_spec.rb +154 -0
  42. data/spec/filters/integration/jdbcstreaming_spec.rb +173 -0
  43. data/spec/filters/jdbc/column_spec.rb +70 -0
  44. data/spec/filters/jdbc/db_object_spec.rb +81 -0
  45. data/spec/filters/jdbc/loader_spec.rb +77 -0
  46. data/spec/filters/jdbc/lookup_processor_spec.rb +132 -0
  47. data/spec/filters/jdbc/lookup_spec.rb +253 -0
  48. data/spec/filters/jdbc/read_only_database_spec.rb +67 -0
  49. data/spec/filters/jdbc/read_write_database_spec.rb +90 -0
  50. data/spec/filters/jdbc/repeating_load_runner_spec.rb +24 -0
  51. data/spec/filters/jdbc/single_load_runner_spec.rb +16 -0
  52. data/spec/filters/jdbc_static_file_local_spec.rb +83 -0
  53. data/spec/filters/jdbc_static_spec.rb +162 -0
  54. data/spec/filters/jdbc_streaming_spec.rb +350 -0
  55. data/spec/filters/remote_server_helper.rb +24 -0
  56. data/spec/filters/shared_helpers.rb +34 -0
  57. data/spec/helpers/WHY-THIS-JAR.txt +4 -0
  58. data/spec/helpers/derbyrun.jar +0 -0
  59. data/spec/inputs/integration/integ_spec.rb +78 -0
  60. data/spec/inputs/jdbc_spec.rb +1431 -0
  61. data/vendor/jar-dependencies/org/apache/derby/derby/10.14.1.0/derby-10.14.1.0.jar +0 -0
  62. data/vendor/jar-dependencies/org/apache/derby/derbyclient/10.14.1.0/derbyclient-10.14.1.0.jar +0 -0
  63. metadata +319 -0
@@ -0,0 +1,573 @@
1
+ :plugin: jdbc
2
+ :type: input
3
+ :default_codec: plain
4
+
5
+ ///////////////////////////////////////////
6
+ START - GENERATED VARIABLES, DO NOT EDIT!
7
+ ///////////////////////////////////////////
8
+ :version: %VERSION%
9
+ :release_date: %RELEASE_DATE%
10
+ :changelog_url: %CHANGELOG_URL%
11
+ :include_path: ../../../../logstash/docs/include
12
+ ///////////////////////////////////////////
13
+ END - GENERATED VARIABLES, DO NOT EDIT!
14
+ ///////////////////////////////////////////
15
+
16
+ [id="plugins-{type}s-{plugin}"]
17
+
18
+ === Jdbc input plugin
19
+
20
+ include::{include_path}/plugin_header.asciidoc[]
21
+
22
+ ==== Description
23
+
24
+ This plugin was created as a way to ingest data in any database
25
+ with a JDBC interface into Logstash. You can periodically schedule ingestion
26
+ using a cron syntax (see `schedule` setting) or run the query one time to load
27
+ data into Logstash. Each row in the resultset becomes a single event.
28
+ Columns in the resultset are converted into fields in the event.
29
+
30
+ ==== Drivers
31
+
32
+ This plugin does not come packaged with JDBC driver libraries. The desired
33
+ jdbc driver library must be explicitly passed in to the plugin using the
34
+ `jdbc_driver_library` configuration option.
35
+
36
+ See the <<plugins-{type}s-{plugin}-jdbc_driver_library>> and <<plugins-{type}s-{plugin}-jdbc_driver_class>>
37
+ options for more info.
38
+
39
+ ==== Scheduling
40
+
41
+ Input from this plugin can be scheduled to run periodically according to a specific
42
+ schedule. This scheduling syntax is powered by https://github.com/jmettraux/rufus-scheduler[rufus-scheduler].
43
+ The syntax is cron-like with some extensions specific to Rufus (e.g. timezone support ).
44
+
45
+ Examples:
46
+
47
+ |==========================================================
48
+ | `* 5 * 1-3 *` | will execute every minute of 5am every day of January through March.
49
+ | `0 * * * *` | will execute on the 0th minute of every hour every day.
50
+ | `0 6 * * * America/Chicago` | will execute at 6:00am (UTC/GMT -5) every day.
51
+ |==========================================================
52
+
53
+
54
+ Further documentation describing this syntax can be found https://github.com/jmettraux/rufus-scheduler#parsing-cronlines-and-time-strings[here].
55
+
56
+ ==== State
57
+
58
+ The plugin will persist the `sql_last_value` parameter in the form of a
59
+ metadata file stored in the configured `last_run_metadata_path`. Upon query execution,
60
+ this file will be updated with the current value of `sql_last_value`. Next time
61
+ the pipeline starts up, this value will be updated by reading from the file. If
62
+ `clean_run` is set to true, this value will be ignored and `sql_last_value` will be
63
+ set to Jan 1, 1970, or 0 if `use_column_value` is true, as if no query has ever been executed.
64
+
65
+ ==== Dealing With Large Result-sets
66
+
67
+ Many JDBC drivers use the `fetch_size` parameter to limit how many
68
+ results are pre-fetched at a time from the cursor into the client's cache
69
+ before retrieving more results from the result-set. This is configured in
70
+ this plugin using the `jdbc_fetch_size` configuration option. No fetch size
71
+ is set by default in this plugin, so the specific driver's default size will
72
+ be used.
73
+
74
+ ==== Usage:
75
+
76
+ Here is an example of setting up the plugin to fetch data from a MySQL database.
77
+ First, we place the appropriate JDBC driver library in our current
78
+ path (this can be placed anywhere on your filesystem). In this example, we connect to
79
+ the 'mydb' database using the user: 'mysql' and wish to input all rows in the 'songs'
80
+ table that match a specific artist. The following examples demonstrates a possible
81
+ Logstash configuration for this. The `schedule` option in this example will
82
+ instruct the plugin to execute this input statement on the minute, every minute.
83
+
84
+ [source,ruby]
85
+ ------------------------------------------------------------------------------
86
+ input {
87
+ jdbc {
88
+ jdbc_driver_library => "mysql-connector-java-5.1.36-bin.jar"
89
+ jdbc_driver_class => "com.mysql.jdbc.Driver"
90
+ jdbc_connection_string => "jdbc:mysql://localhost:3306/mydb"
91
+ jdbc_user => "mysql"
92
+ parameters => { "favorite_artist" => "Beethoven" }
93
+ schedule => "* * * * *"
94
+ statement => "SELECT * from songs where artist = :favorite_artist"
95
+ }
96
+ }
97
+ ------------------------------------------------------------------------------
98
+
99
+ ==== Configuring SQL statement
100
+
101
+ A sql statement is required for this input. This can be passed-in via a
102
+ statement option in the form of a string, or read from a file (`statement_filepath`). File
103
+ option is typically used when the SQL statement is large or cumbersome to supply in the config.
104
+ The file option only supports one SQL statement. The plugin will only accept one of the options.
105
+ It cannot read a statement from a file as well as from the `statement` configuration parameter.
106
+
107
+ ==== Configuring multiple SQL statements
108
+
109
+ Configuring multiple SQL statements is useful when there is a need to query and ingest data
110
+ from different database tables or views. It is possible to define separate Logstash
111
+ configuration files for each statement or to define multiple statements in a single configuration
112
+ file. When using multiple statements in a single Logstash configuration file, each statement
113
+ has to be defined as a separate jdbc input (including jdbc driver, connection string and other
114
+ required parameters).
115
+
116
+ Please note that if any of the statements use the `sql_last_value` parameter (e.g. for
117
+ ingesting only data changed since last run), each input should define its own
118
+ `last_run_metadata_path` parameter. Failure to do so will result in undesired behaviour, as
119
+ all inputs will store their state to the same (default) metadata file, effectively
120
+ overwriting each other's `sql_last_value`.
121
+
122
+ ==== Predefined Parameters
123
+
124
+ Some parameters are built-in and can be used from within your queries.
125
+ Here is the list:
126
+
127
+ |==========================================================
128
+ |sql_last_value | The value used to calculate which rows to query. Before any query is run,
129
+ this is set to Thursday, 1 January 1970, or 0 if `use_column_value` is true and
130
+ `tracking_column` is set. It is updated accordingly after subsequent queries are run.
131
+ |==========================================================
132
+
133
+ Example:
134
+ [source,ruby]
135
+ ---------------------------------------------------------------------------------------------------
136
+ input {
137
+ jdbc {
138
+ statement => "SELECT id, mycolumn1, mycolumn2 FROM my_table WHERE id > :sql_last_value"
139
+ use_column_value => true
140
+ tracking_column => "id"
141
+ # ... other configuration bits
142
+ }
143
+ }
144
+ ---------------------------------------------------------------------------------------------------
145
+
146
+ ==== Prepared Statements
147
+
148
+ Using server side prepared statements can speed up execution times as the server optimises the query plan and execution.
149
+
150
+ NOTE: Not all JDBC accessible technologies will support prepared statements.
151
+
152
+ With the introduction of Prepared Statement support comes a different code execution path and some new settings. Most of the existing settings are still useful but there are several new settings for Prepared Statements to read up on.
153
+ Use the boolean setting `use_prepared_statements` to enable this execution mode. Use the `prepared_statement_name` setting to specify a name for the Prepared Statement, this identifies the prepared statement locally and remotely and it should be unique in your config and on the database. Use the `prepared_statement_bind_values` array setting to specify the bind values, use the exact string `:sql_last_value` (multiple times if necessary) for the predefined parameter mentioned before. The `statement` (or `statement_path`) setting still holds the SQL statement but to use bind variables you must use the `?` character as a placeholder in the exact order found in the `prepared_statement_bind_values` array.
154
+
155
+ NOTE: Building count queries around a prepared statement is not supported at this time and because jdbc paging uses count queries under the hood, jdbc paging is not supported with prepared statements at this time either. Therefore, `jdbc_paging_enabled`, `jdbc_page_size` settings are ignored when using prepared statements.
156
+
157
+ Example:
158
+ [source,ruby]
159
+ ---------------------------------------------------------------------------------------------------
160
+ input {
161
+ jdbc {
162
+ statement => "SELECT * FROM mgd.seq_sequence WHERE _sequence_key > ? AND _sequence_key < ? + ? ORDER BY _sequence_key ASC"
163
+ prepared_statement_bind_values => [":sql_last_value", ":sql_last_value", 4]
164
+ prepared_statement_name => "foobar"
165
+ use_prepared_statements => true
166
+ use_column_value => true
167
+ tracking_column_type => "numeric"
168
+ tracking_column => "_sequence_key"
169
+ last_run_metadata_path => "/elastic/tmp/testing/confs/test-jdbc-int-sql_last_value.yml"
170
+ # ... other configuration bits
171
+ }
172
+ }
173
+ ---------------------------------------------------------------------------------------------------
174
+
175
+
176
+ [id="plugins-{type}s-{plugin}-options"]
177
+ ==== Jdbc Input Configuration Options
178
+
179
+ This plugin supports the following configuration options plus the <<plugins-{type}s-{plugin}-common-options>> described later.
180
+
181
+ [cols="<,<,<",options="header",]
182
+ |=======================================================================
183
+ |Setting |Input type|Required
184
+ | <<plugins-{type}s-{plugin}-clean_run>> |<<boolean,boolean>>|No
185
+ | <<plugins-{type}s-{plugin}-columns_charset>> |<<hash,hash>>|No
186
+ | <<plugins-{type}s-{plugin}-connection_retry_attempts>> |<<number,number>>|No
187
+ | <<plugins-{type}s-{plugin}-connection_retry_attempts_wait_time>> |<<number,number>>|No
188
+ | <<plugins-{type}s-{plugin}-jdbc_connection_string>> |<<string,string>>|Yes
189
+ | <<plugins-{type}s-{plugin}-jdbc_default_timezone>> |<<string,string>>|No
190
+ | <<plugins-{type}s-{plugin}-jdbc_driver_class>> |<<string,string>>|Yes
191
+ | <<plugins-{type}s-{plugin}-jdbc_driver_library>> |<<string,string>>|No
192
+ | <<plugins-{type}s-{plugin}-jdbc_fetch_size>> |<<number,number>>|No
193
+ | <<plugins-{type}s-{plugin}-jdbc_page_size>> |<<number,number>>|No
194
+ | <<plugins-{type}s-{plugin}-jdbc_paging_enabled>> |<<boolean,boolean>>|No
195
+ | <<plugins-{type}s-{plugin}-jdbc_password>> |<<password,password>>|No
196
+ | <<plugins-{type}s-{plugin}-jdbc_password_filepath>> |a valid filesystem path|No
197
+ | <<plugins-{type}s-{plugin}-jdbc_pool_timeout>> |<<number,number>>|No
198
+ | <<plugins-{type}s-{plugin}-jdbc_user>> |<<string,string>>|Yes
199
+ | <<plugins-{type}s-{plugin}-jdbc_validate_connection>> |<<boolean,boolean>>|No
200
+ | <<plugins-{type}s-{plugin}-jdbc_validation_timeout>> |<<number,number>>|No
201
+ | <<plugins-{type}s-{plugin}-last_run_metadata_path>> |<<string,string>>|No
202
+ | <<plugins-{type}s-{plugin}-lowercase_column_names>> |<<boolean,boolean>>|No
203
+ | <<plugins-{type}s-{plugin}-parameters>> |<<hash,hash>>|No
204
+ | <<plugins-{type}s-{plugin}-plugin_timezone>> |<<string,string>>, one of `["local", "utc"]`|No
205
+ | <<plugins-{type}s-{plugin}-prepared_statement_bind_values>> |<<array,array>>|No
206
+ | <<plugins-{type}s-{plugin}-prepared_statement_name>> |<<string,string>>|No
207
+ | <<plugins-{type}s-{plugin}-record_last_run>> |<<boolean,boolean>>|No
208
+ | <<plugins-{type}s-{plugin}-schedule>> |<<string,string>>|No
209
+ | <<plugins-{type}s-{plugin}-sequel_opts>> |<<hash,hash>>|No
210
+ | <<plugins-{type}s-{plugin}-sql_log_level>> |<<string,string>>, one of `["fatal", "error", "warn", "info", "debug"]`|No
211
+ | <<plugins-{type}s-{plugin}-statement>> |<<string,string>>|No
212
+ | <<plugins-{type}s-{plugin}-statement_filepath>> |a valid filesystem path|No
213
+ | <<plugins-{type}s-{plugin}-tracking_column>> |<<string,string>>|No
214
+ | <<plugins-{type}s-{plugin}-tracking_column_type>> |<<string,string>>, one of `["numeric", "timestamp"]`|No
215
+ | <<plugins-{type}s-{plugin}-use_column_value>> |<<boolean,boolean>>|No
216
+ | <<plugins-{type}s-{plugin}-use_prepared_statements>> |<<boolean,boolean>>|No
217
+ |=======================================================================
218
+
219
+ Also see <<plugins-{type}s-{plugin}-common-options>> for a list of options supported by all
220
+ input plugins.
221
+
222
+ &nbsp;
223
+
224
+ [id="plugins-{type}s-{plugin}-clean_run"]
225
+ ===== `clean_run`
226
+
227
+ * Value type is <<boolean,boolean>>
228
+ * Default value is `false`
229
+
230
+ Whether the previous run state should be preserved
231
+
232
+ [id="plugins-{type}s-{plugin}-columns_charset"]
233
+ ===== `columns_charset`
234
+
235
+ * Value type is <<hash,hash>>
236
+ * Default value is `{}`
237
+
238
+ The character encoding for specific columns. This option will override the `:charset` option
239
+ for the specified columns.
240
+
241
+ Example:
242
+ [source,ruby]
243
+ -------------------------------------------------------
244
+ input {
245
+ jdbc {
246
+ ...
247
+ columns_charset => { "column0" => "ISO-8859-1" }
248
+ ...
249
+ }
250
+ }
251
+ -------------------------------------------------------
252
+ this will only convert column0 that has ISO-8859-1 as an original encoding.
253
+
254
+ [id="plugins-{type}s-{plugin}-connection_retry_attempts"]
255
+ ===== `connection_retry_attempts`
256
+
257
+ * Value type is <<number,number>>
258
+ * Default value is `1`
259
+
260
+ Maximum number of times to try connecting to database
261
+
262
+ [id="plugins-{type}s-{plugin}-connection_retry_attempts_wait_time"]
263
+ ===== `connection_retry_attempts_wait_time`
264
+
265
+ * Value type is <<number,number>>
266
+ * Default value is `0.5`
267
+
268
+ Number of seconds to sleep between connection attempts
269
+
270
+ [id="plugins-{type}s-{plugin}-jdbc_connection_string"]
271
+ ===== `jdbc_connection_string`
272
+
273
+ * This is a required setting.
274
+ * Value type is <<string,string>>
275
+ * There is no default value for this setting.
276
+
277
+ JDBC connection string
278
+
279
+ [id="plugins-{type}s-{plugin}-jdbc_default_timezone"]
280
+ ===== `jdbc_default_timezone`
281
+
282
+ * Value type is <<string,string>>
283
+ * There is no default value for this setting.
284
+
285
+ Timezone conversion.
286
+ Logstash (and Elasticsearch) expects that timestamps are expressed in UTC terms.
287
+ If your database has recorded timestamps that are relative to another timezone,
288
+ the database timezone if you will, then set this setting to be the timezone that
289
+ the database is using. However, as SQL does not allow for timezone data in
290
+ timestamp fields we can't figure this out on a record by record basis. This plugin
291
+ will automatically convert your SQL timestamp fields to Logstash timestamps,
292
+ in relative UTC time in ISO8601 format.
293
+
294
+ Using this setting will manually assign a specified timezone offset, instead
295
+ of using the timezone setting of the local machine. You must use a canonical
296
+ timezone, *America/Denver*, for example.
297
+
298
+ [id="plugins-{type}s-{plugin}-plugin_timezone"]
299
+ ===== `plugin_timezone`
300
+
301
+ * Value can be any of: `utc`, `local`
302
+ * Default value is `"utc"`
303
+
304
+ If you want this plugin to offset timestamps to a timezone other than UTC, you
305
+ can set this setting to `local` and the plugin will use the OS timezone for offset
306
+ adjustments.
307
+
308
+ Note: when specifying `plugin_timezone` and/or `jdbc_default_timezone`, offset
309
+ adjustments are made in two places, if `sql_last_value` is a timestamp and it
310
+ is used as a parameter in the statement then offset adjustment is done from the
311
+ plugin timezone into the data timezone and while records are processed, timestamps
312
+ are offset adjusted from the database timezone to the plugin timezone. If your
313
+ database timezone is UTC then you do not need to set either of these settings.
314
+
315
+ [id="plugins-{type}s-{plugin}-jdbc_driver_class"]
316
+ ===== `jdbc_driver_class`
317
+
318
+ * This is a required setting.
319
+ * Value type is <<string,string>>
320
+ * There is no default value for this setting.
321
+
322
+ JDBC driver class to load, for example, "org.apache.derby.jdbc.ClientDriver"
323
+
324
+ NOTE: Per https://github.com/logstash-plugins/logstash-input-jdbc/issues/43, prepending `Java::` to the driver class
325
+ may be required if it appears that the driver is not being loaded correctly despite relevant jar(s) being provided by
326
+ either via the `jdbc_driver_library` setting or being placed in the Logstash Java classpath. This is known to be the
327
+ case for the Oracle JDBC driver (ojdbc6.jar), where the correct `jdbc_driver_class` is
328
+ `"Java::oracle.jdbc.driver.OracleDriver"`, and may also be the case for other JDBC drivers.
329
+
330
+ [id="plugins-{type}s-{plugin}-jdbc_driver_library"]
331
+ ===== `jdbc_driver_library`
332
+
333
+ * Value type is <<string,string>>
334
+ * There is no default value for this setting.
335
+
336
+ JDBC driver library path to third party driver library. In case of multiple libraries being
337
+ required you can pass them separated by a comma.
338
+
339
+ NOTE: If not provided, Plugin will look for the driver class in the Logstash Java classpath. Additionally, if the library
340
+ does not appear to be being loaded correctly via this setting, placing the relevant jar(s) in the Logstash Java
341
+ classpath rather than via this setting may help.
342
+
343
+ [id="plugins-{type}s-{plugin}-jdbc_fetch_size"]
344
+ ===== `jdbc_fetch_size`
345
+
346
+ * Value type is <<number,number>>
347
+ * There is no default value for this setting.
348
+
349
+ JDBC fetch size. if not provided, respective driver's default will be used
350
+
351
+ [id="plugins-{type}s-{plugin}-jdbc_page_size"]
352
+ ===== `jdbc_page_size`
353
+
354
+ * Value type is <<number,number>>
355
+ * Default value is `100000`
356
+
357
+ JDBC page size
358
+
359
+ [id="plugins-{type}s-{plugin}-jdbc_paging_enabled"]
360
+ ===== `jdbc_paging_enabled`
361
+
362
+ * Value type is <<boolean,boolean>>
363
+ * Default value is `false`
364
+
365
+ JDBC enable paging
366
+
367
+ This will cause a sql statement to be broken up into multiple queries.
368
+ Each query will use limits and offsets to collectively retrieve the full
369
+ result-set. The limit size is set with `jdbc_page_size`.
370
+
371
+ Be aware that ordering is not guaranteed between queries.
372
+
373
+ [id="plugins-{type}s-{plugin}-jdbc_password"]
374
+ ===== `jdbc_password`
375
+
376
+ * Value type is <<password,password>>
377
+ * There is no default value for this setting.
378
+
379
+ JDBC password
380
+
381
+ [id="plugins-{type}s-{plugin}-jdbc_password_filepath"]
382
+ ===== `jdbc_password_filepath`
383
+
384
+ * Value type is <<path,path>>
385
+ * There is no default value for this setting.
386
+
387
+ JDBC password filename
388
+
389
+ [id="plugins-{type}s-{plugin}-jdbc_pool_timeout"]
390
+ ===== `jdbc_pool_timeout`
391
+
392
+ * Value type is <<number,number>>
393
+ * Default value is `5`
394
+
395
+ Connection pool configuration.
396
+ The amount of seconds to wait to acquire a connection before raising a PoolTimeoutError (default 5)
397
+
398
+ [id="plugins-{type}s-{plugin}-jdbc_user"]
399
+ ===== `jdbc_user`
400
+
401
+ * This is a required setting.
402
+ * Value type is <<string,string>>
403
+ * There is no default value for this setting.
404
+
405
+ JDBC user
406
+
407
+ [id="plugins-{type}s-{plugin}-jdbc_validate_connection"]
408
+ ===== `jdbc_validate_connection`
409
+
410
+ * Value type is <<boolean,boolean>>
411
+ * Default value is `false`
412
+
413
+ Connection pool configuration.
414
+ Validate connection before use.
415
+
416
+ [id="plugins-{type}s-{plugin}-jdbc_validation_timeout"]
417
+ ===== `jdbc_validation_timeout`
418
+
419
+ * Value type is <<number,number>>
420
+ * Default value is `3600`
421
+
422
+ Connection pool configuration.
423
+ How often to validate a connection (in seconds)
424
+
425
+ [id="plugins-{type}s-{plugin}-last_run_metadata_path"]
426
+ ===== `last_run_metadata_path`
427
+
428
+ * Value type is <<string,string>>
429
+ * Default value is `"$HOME/.logstash_jdbc_last_run"`
430
+
431
+ Path to file with last run time
432
+
433
+ [id="plugins-{type}s-{plugin}-lowercase_column_names"]
434
+ ===== `lowercase_column_names`
435
+
436
+ * Value type is <<boolean,boolean>>
437
+ * Default value is `true`
438
+
439
+ Whether to force the lowercasing of identifier fields
440
+
441
+ [id="plugins-{type}s-{plugin}-parameters"]
442
+ ===== `parameters`
443
+
444
+ * Value type is <<hash,hash>>
445
+ * Default value is `{}`
446
+
447
+ Hash of query parameter, for example `{ "target_id" => "321" }`
448
+
449
+ [id="plugins-{type}s-{plugin}-prepared_statement_bind_values"]
450
+ ===== `prepared_statement_bind_values`
451
+
452
+ * Value type is <<array,array>>
453
+ * Default value is `[]`
454
+
455
+ Array of bind values for the prepared statement. `:sql_last_value` is a reserved predefined string
456
+
457
+ [id="plugins-{type}s-{plugin}-prepared_statement_name"]
458
+ ===== `prepared_statement_name`
459
+
460
+ * Value type is <<string,string>>
461
+ * Default value is `""`
462
+
463
+ Name given to the prepared statement. It must be unique in your config and in the database
464
+
465
+ [id="plugins-{type}s-{plugin}-record_last_run"]
466
+ ===== `record_last_run`
467
+
468
+ * Value type is <<boolean,boolean>>
469
+ * Default value is `true`
470
+
471
+ Whether to save state or not in <<plugins-{type}s-{plugin}-last_run_metadata_path>>
472
+
473
+ [id="plugins-{type}s-{plugin}-schedule"]
474
+ ===== `schedule`
475
+
476
+ * Value type is <<string,string>>
477
+ * There is no default value for this setting.
478
+
479
+ Schedule of when to periodically run statement, in Cron format
480
+ for example: "* * * * *" (execute query every minute, on the minute)
481
+
482
+ There is no schedule by default. If no schedule is given, then the statement is run
483
+ exactly once.
484
+
485
+ [id="plugins-{type}s-{plugin}-sequel_opts"]
486
+ ===== `sequel_opts`
487
+
488
+ * Value type is <<hash,hash>>
489
+ * Default value is `{}`
490
+
491
+ General/Vendor-specific Sequel configuration options.
492
+
493
+ An example of an optional connection pool configuration
494
+ max_connections - The maximum number of connections the connection pool
495
+
496
+ examples of vendor-specific options can be found in this
497
+ documentation page: https://github.com/jeremyevans/sequel/blob/master/doc/opening_databases.rdoc
498
+
499
+ [id="plugins-{type}s-{plugin}-sql_log_level"]
500
+ ===== `sql_log_level`
501
+
502
+ * Value can be any of: `fatal`, `error`, `warn`, `info`, `debug`
503
+ * Default value is `"info"`
504
+
505
+ Log level at which to log SQL queries, the accepted values are the common ones fatal, error, warn,
506
+ info and debug. The default value is info.
507
+
508
+ [id="plugins-{type}s-{plugin}-statement"]
509
+ ===== `statement`
510
+
511
+ * Value type is <<string,string>>
512
+ * There is no default value for this setting.
513
+
514
+ If undefined, Logstash will complain, even if codec is unused.
515
+ Statement to execute
516
+
517
+ To use parameters, use named parameter syntax.
518
+ For example:
519
+
520
+ [source, ruby]
521
+ -----------------------------------------------
522
+ "SELECT * FROM MYTABLE WHERE id = :target_id"
523
+ -----------------------------------------------
524
+
525
+ here, ":target_id" is a named parameter. You can configure named parameters
526
+ with the `parameters` setting.
527
+
528
+ [id="plugins-{type}s-{plugin}-statement_filepath"]
529
+ ===== `statement_filepath`
530
+
531
+ * Value type is <<path,path>>
532
+ * There is no default value for this setting.
533
+
534
+ Path of file containing statement to execute
535
+
536
+ [id="plugins-{type}s-{plugin}-tracking_column"]
537
+ ===== `tracking_column`
538
+
539
+ * Value type is <<string,string>>
540
+ * There is no default value for this setting.
541
+
542
+ The column whose value is to be tracked if `use_column_value` is set to `true`
543
+
544
+ [id="plugins-{type}s-{plugin}-tracking_column_type"]
545
+ ===== `tracking_column_type`
546
+
547
+ * Value can be any of: `numeric`, `timestamp`
548
+ * Default value is `"numeric"`
549
+
550
+ Type of tracking column. Currently only "numeric" and "timestamp"
551
+
552
+ [id="plugins-{type}s-{plugin}-use_column_value"]
553
+ ===== `use_column_value`
554
+
555
+ * Value type is <<boolean,boolean>>
556
+ * Default value is `false`
557
+
558
+ When set to `true`, uses the defined
559
+ <<plugins-{type}s-{plugin}-tracking_column>> value as the `:sql_last_value`. When set
560
+ to `false`, `:sql_last_value` reflects the last time the query was executed.
561
+
562
+ [id="plugins-{type}s-{plugin}-use_prepared_statements"]
563
+ ===== `use_prepared_statements`
564
+
565
+ * Value type is <<boolean,boolean>>
566
+ * Default value is `false`
567
+
568
+ When set to `true`, enables prepare statement usage
569
+
570
+ [id="plugins-{type}s-{plugin}-common-options"]
571
+ include::{include_path}/{type}.asciidoc[]
572
+
573
+ :default_codec!:
@@ -0,0 +1,125 @@
1
+ # encoding: utf-8
2
+ require "fileutils"
3
+ require "sequel"
4
+ require "sequel/adapters/jdbc"
5
+ require "java"
6
+ require "logstash/util/loggable"
7
+
8
+ module LogStash module Filters module Jdbc
9
+ EMBEDDED_DERBY_DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver".freeze
10
+ MEMORY_DERBY_LOCAL_CONNECTION_STRING = "jdbc:derby:memory:____;create=true".freeze
11
+ CONNECTION_ERRORS = [::Sequel::DatabaseConnectionError, ::Sequel::DatabaseDisconnectError, ::Sequel::PoolTimeout]
12
+
13
+ class LookupJdbcException < ::StandardError; end
14
+ class LoaderJdbcException < ::StandardError; end
15
+ class ConnectionJdbcException < ::StandardError; end
16
+
17
+ class BasicDatabase
18
+ include LogStash::Util::Loggable
19
+
20
+ def self.wrap_error(new_error_class, exception, message = nil)
21
+ error = new_error_class.new(message || exception.message)
22
+ error.set_backtrace(exception.backtrace)
23
+ error
24
+ end
25
+
26
+ def self.create(
27
+ connection_string = MEMORY_DERBY_LOCAL_CONNECTION_STRING,
28
+ driver_class = EMBEDDED_DERBY_DRIVER_CLASS,
29
+ driver_library = nil,
30
+ user = nil,
31
+ password = nil)
32
+ instance = new
33
+ instance.post_create(connection_string, driver_class, driver_library, user, password)
34
+ instance
35
+ end
36
+
37
+ def self.random_name(length = 10)
38
+ SecureRandom.hex(length)
39
+ end
40
+
41
+ attr_reader :unique_db_name
42
+
43
+ def initialize()
44
+ @options_hash = {}
45
+ post_initialize
46
+ end
47
+
48
+ def connect(err_message)
49
+ begin
50
+ @db = ::Sequel.connect(@connection_string, @options_hash)
51
+ rescue *CONNECTION_ERRORS => err
52
+ # we do not raise an error when there is a connection error, we hope that the connection works next time
53
+ logger.error(err_message, :exception => err.message, :backtrace => err.backtrace.take(8))
54
+ else
55
+ raise "::Sequel.connect returned a nil db instance, connection_string: #{@connection_string}, options: #{@options_hash.inspect}" if @db.nil?
56
+ end
57
+ end
58
+
59
+ def disconnect(err_message)
60
+ return if @db.nil?
61
+ begin
62
+ @db.disconnect
63
+ rescue *CONNECTION_ERRORS => err
64
+ # we do not raise an error when there is a connection error, we hope that the connection works next time
65
+ logger.error(err_message, :exception => err.message, :backtrace => err.backtrace.take(8))
66
+ ensure
67
+ @db = nil
68
+ end
69
+ end
70
+
71
+ def connected?
72
+ !@db.nil?
73
+ end
74
+
75
+ def empty_record_set
76
+ []
77
+ end
78
+
79
+ def post_create(connection_string, driver_class, driver_library, user, password)
80
+ raise NotImplementedError.new("#{self.class.name} is abstract, you must subclass it and implement #post_create()")
81
+ end
82
+
83
+ private
84
+
85
+ def verify_connection(connection_string, driver_class, driver_library, user, password)
86
+ begin
87
+ if driver_library
88
+ driver_library.split(",").each do |driver_path|
89
+ require driver_path
90
+ end
91
+ end
92
+ rescue LoadError => e
93
+ msg = "The driver library cannot be loaded. The system error was: '#{e.message}'."
94
+ raise wrap_error(ConnectionJdbcException, e, msg)
95
+ end
96
+ begin
97
+ db = nil
98
+ ::Sequel::JDBC.load_driver(driver_class)
99
+ @connection_string = connection_string
100
+ if user
101
+ @options_hash[:user] = user
102
+ end
103
+ if password
104
+ @options_hash[:password] = password.value
105
+ end
106
+ # test the connection as early as possible
107
+ db = ::Sequel.connect(@connection_string, {:test => true}.merge(@options_hash))
108
+ rescue ::Sequel::AdapterNotFound => anf
109
+ raise wrap_error(ConnectionJdbcException, anf)
110
+ rescue ::Sequel::DatabaseConnectionError => dce
111
+ raise wrap_error(ConnectionJdbcException, dce)
112
+ ensure
113
+ db.disconnect unless db.nil?
114
+ end
115
+ end
116
+
117
+ def post_initialize()
118
+ @unique_db_name = SecureRandom.hex(12)
119
+ end
120
+
121
+ def wrap_error(new_error_class, exception, message = nil)
122
+ self.class.wrap_error(new_error_class, exception, message)
123
+ end
124
+ end
125
+ end end end