For other versions, see theVersioned plugin docs.
For questions about the plugin, open a topic in the Discuss forums. For bugs or feature requests, open an issue in Github.For the list of Elastic supported plugins, please consult the Elastic Support Matrix.
This plugin was created as a way to ingest data in any databasewith a JDBC interface into Logstash. You can periodically schedule ingestionusing a cron syntax (see schedule
setting) or run the query one time to loaddata into Logstash. Each row in the resultset becomes a single event.Columns in the resultset are converted into fields in the event.
This plugin does not come packaged with JDBC driver libraries. The desiredjdbc driver library must be explicitly passed in to the plugin using thejdbc_driver_library
configuration option.
Input from this plugin can be scheduled to run periodically according to a specificschedule. This scheduling syntax is powered by rufus-scheduler.The syntax is cron-like with some extensions specific to Rufus (e.g. timezone support ).
Examples:
|
will execute every minute of 5am every day of January through March. |
|
will execute on the 0th minute of every hour every day. |
|
will execute at 6:00am (UTC/GMT -5) every day. |
Further documentation describing this syntax can be found here.
The plugin will persist the sql_last_value
parameter in the form of ametadata file stored in the configured last_run_metadata_path
. Upon query execution,this file will be updated with the current value of sql_last_value
. Next timethe pipeline starts up, this value will be updated by reading from the file. Ifclean_run
is set to true, this value will be ignored and sql_last_value
will beset to Jan 1, 1970, or 0 if use_column_value
is true, as if no query has ever been executed.
Many JDBC drivers use the fetch_size
parameter to limit how manyresults are pre-fetched at a time from the cursor into the client’s cachebefore retrieving more results from the result-set. This is configured inthis plugin using the jdbc_fetch_size
configuration option. No fetch sizeis set by default in this plugin, so the specific driver’s default size willbe used.
Here is an example of setting up the plugin to fetch data from a MySQL database.First, we place the appropriate JDBC driver library in our currentpath (this can be placed anywhere on your filesystem). In this example, we connect tothe mydb database using the user: mysql and wish to input all rows in the songstable that match a specific artist. The following examples demonstrates a possibleLogstash configuration for this. The schedule
option in this example willinstruct the plugin to execute this input statement on the minute, every minute.
input { jdbc { jdbc_driver_library => "mysql-connector-java-5.1.36-bin.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_connection_string => "jdbc:mysql://localhost:3306/mydb" jdbc_user => "mysql" parameters => { "favorite_artist" => "Beethoven" } schedule => "* * * * *" statement => "SELECT * from songs where artist = :favorite_artist" }}
A sql statement is required for this input. This can be passed-in via astatement option in the form of a string, or read from a file (statement_filepath
). Fileoption is typically used when the SQL statement is large or cumbersome to supply in the config.The file option only supports one SQL statement. The plugin will only accept one of the options.It cannot read a statement from a file as well as from the statement
configuration parameter.
Configuring multiple SQL statements is useful when there is a need to query and ingest datafrom different database tables or views. It is possible to define separate Logstashconfiguration files for each statement or to define multiple statements in a single configurationfile. When using multiple statements in a single Logstash configuration file, each statementhas to be defined as a separate jdbc input (including jdbc driver, connection string and otherrequired parameters).
Please note that if any of the statements use the sql_last_value
parameter (e.g. foringesting only data changed since last run), each input should define its ownlast_run_metadata_path
parameter. Failure to do so will result in undesired behaviour, asall inputs will store their state to the same (default) metadata file, effectivelyoverwriting each other’s sql_last_value
.
Some parameters are built-in and can be used from within your queries.Here is the list:
sql_last_value |
The value used to calculate which rows to query. Before any query is run,this is set to Thursday, 1 January 1970, or 0 if |
Example:
input { jdbc { statement => "SELECT id, mycolumn1, mycolumn2 FROM my_table WHERE id > :sql_last_value" use_column_value => true tracking_column => "id" # ... other configuration bits }}
This plugin supports the following configuration options plus the Common Options described later.
Setting | Input type | Required |
---|---|---|
No |
||
No |
||
No |
||
No |
||
Yes |
||
No |
||
Yes |
||
No |
||
No |
||
No |
||
No |
||
No |
||
a valid filesystem path |
No |
|
No |
||
Yes |
||
No |
||
No |
||
No |
||
No |
||
No |
||
No |
||
No |
||
No |
||
string, one of |
No |
|
No |
||
a valid filesystem path |
No |
|
No |
||
string, one of |
No |
|
No |
Also see Common Options for a list of options supported by allinput plugins.
false
Whether the previous run state should be preserved
{}
The character encoding for specific columns. This option will override the :charset
optionfor the specified columns.
Example:
input { jdbc { ... columns_charset => { "column0" => "ISO-8859-1" } ... }}
this will only convert column0 that has ISO-8859-1 as an original encoding.
1
Maximum number of times to try connecting to database
0.5
Number of seconds to sleep between connection attempts
JDBC connection string
Timezone conversion.SQL does not allow for timezone data in timestamp fields. This plugin will automaticallyconvert your SQL timestamp fields to Logstash timestamps, in relative UTC time in ISO8601 format.
Using this setting will manually assign a specified timezone offset, insteadof using the timezone setting of the local machine. You must use a canonicaltimezone, America/Denver, for example.
JDBC driver class to load, for exmaple, "org.apache.derby.jdbc.ClientDriver"NB per https://github.com/logstash-plugins/logstash-input-jdbc/issues/43 if you are usingthe Oracle JDBC driver (ojdbc6.jar) the correct jdbc_driver_class
is "Java::oracle.jdbc.driver.OracleDriver"
Tentative of abstracting JDBC logic to a mixinfor potential reuse in other plugins (input/output)This method is called when someone includes this moduleAdd these methods to the base given.JDBC driver library path to third party driver library. In case of multiple libraries beingrequired you can pass them separated by a comma.
If not provided, Plugin will look for the driver class in the Logstash Java classpath.
JDBC fetch size. if not provided, respective driver’s default will be used
false
JDBC enable paging
This will cause a sql statement to be broken up into multiple queries.Each query will use limits and offsets to collectively retrieve the fullresult-set. The limit size is set with jdbc_page_size
.
Be aware that ordering is not guaranteed between queries.
JDBC password filename
5
Connection pool configuration.The amount of seconds to wait to acquire a connection before raising a PoolTimeoutError (default 5)
JDBC user
false
Connection pool configuration.Validate connection before use.
3600
Connection pool configuration.How often to validate a connection (in seconds)
"$HOME/.logstash_jdbc_last_run"
Path to file with last run time
true
Whether to force the lowercasing of identifier fields
{}
Hash of query parameter, for example { "target_id" => "321" }
true
Whether to save state or not in last_run_metadata_path
Schedule of when to periodically run statement, in Cron formatfor example: "* * * * *" (execute query every minute, on the minute)
There is no schedule by default. If no schedule is given, then the statement is runexactly once.
{}
General/Vendor-specific Sequel configuration options.
An example of an optional connection pool configuration max_connections - The maximum number of connections the connection pool
examples of vendor-specific options can be found in thisdocumentation page: https://github.com/jeremyevans/sequel/blob/master/doc/opening_databases.rdoc
fatal
, error
, warn
, info
, debug
"info"
Log level at which to log SQL queries, the accepted values are the common ones fatal, error, warn,info and debug. The default value is info.
If undefined, Logstash will complain, even if codec is unused.Statement to execute
To use parameters, use named parameter syntax.For example:
"SELECT * FROM MYTABLE WHERE id = :target_id"
here, ":target_id" is a named parameter. You can configure named parameterswith the parameters
setting.
Path of file containing statement to execute
The column whose value is to be tracked if use_column_value
is set to true
numeric
, timestamp
"numeric"
Type of tracking column. Currently only "numeric" and "timestamp"
false
When set to true
, uses the definedtracking_column
value as the :sql_last_value
. When setto false
, :sql_last_value
reflects the last time the query was executed.
The following configuration options are supported by all input plugins:
"plain"
The codec used for input data. Input codecs are a convenient method for decoding your data before it enters the input, without needing a separate filter in your Logstash pipeline.
true
Disable or enable metric logging for this specific plugin instanceby default we record all the metrics we can, but you can disable metrics collectionfor a specific plugin.
Add a unique ID
to the plugin configuration. If no ID is specified, Logstash will generate one.It is strongly recommended to set this ID in your configuration. This is particularly usefulwhen you have two or more plugins of the same type, for example, if you have 2 jdbc inputs.Adding a named ID in this case will help in monitoring Logstash when using the monitoring APIs.
input { jdbc { id => "my_plugin_id" }}
Add any number of arbitrary tags to your event.
This can help with processing later.
Add a type
field to all events handled by this input.
Types are used mainly for filter activation.
The type is stored as part of the event itself, so you canalso use the type to search for it in Kibana.
If you try to set a type on an event that already has one (forexample when you send an event from a shipper to an indexer) thena new input will not override the existing type. A type set atthe shipper stays with that event for its life evenwhen sent to another Logstash server.