logstash-output-jdbc 如何动态插入表,将表名设置为变量

动态插入到表中

表名filelog[变量]值,可以通过设置变量,将读到的记录插入动态表中

input {
    file {
        path => "D:/es/file_log/*.log"
        tags => ["filelogbranch"]
        start_position => "beginning"
        codec => "json"
    }
}

filter {
   ruby { 
    code => "event.set('timestamp', event.get('@timestamp').time.localtime + 8*60*60)" 
   }
   ruby {
    code => "event.set('@timestamp',event.get('timestamp'))"
   }
   mutate {
    remove_field => ["@version","message","host","path","timestamp"]
   }
}

output {
    jdbc {
            driver_jar_path => 'C:\logstash-8.1.2\sqljdbc.jar'
            connection_string => "jdbc:jtds://server:1433;databaseName=CobraDGServer;user=sa;password=123456"
            statement => [ "INSERT INTO FileLog2%{+YYYYMMdd}(id,userId) VALUES(?, ?)", "id", "userId"]
        }
        
}

Configuration options

OptionTypeDescriptionRequired?Default
driver_classStringSpecify a driver class if autoloading failsNo
driver_auto_commitBooleanIf the driver does not support auto commit, you should set this to falseNoTrue
driver_jar_pathStringFile path to jar file containing your JDBC driver. This is optional, and all JDBC jars may be placed in $LOGSTASH_HOME/vendor/jar/jdbc instead.No
connection_stringStringJDBC connection URLYes
connection_testBooleanRun a JDBC connection test. Some drivers do not function correctly, and you may need to disable the connection test to supress an error. Cockroach with the postgres JDBC driver is such an example.NoYes
connection_test_queryStringConnection test and init query string, required for some JDBC drivers that don't support isValid(). Typically you'd set to this "SELECT 1"No
usernameStringJDBC username - this is optional as it may be included in the connection string, for many driversNo
passwordStringJDBC password - this is optional as it may be included in the connection string, for many driversNo
statementArrayAn array of strings representing the SQL statement to run. Index 0 is the SQL statement that is prepared, all other array entries are passed in as parameters (in order). A parameter may either be a property of the event (i.e. "@timestamp", or "host") or a formatted string (i.e. "%{host} - %{message}" or "%{message}"). If a key is passed then it will be automatically converted as required for insertion into SQL. If it's a formatted string then it will be passed in verbatim.Yes
unsafe_statementBooleanIf yes, the statement is evaluated for event fields - this allows you to use dynamic table names, etc. This is highly dangerous and you should not use this unless you are 100% sure that the field(s) you are passing in are 100% safe. Failure to do so will result in possible SQL injections. Example statement: [ "insert into %{table_name_field} (column) values(?)", "fieldname" ]NoFalse
max_pool_sizeNumberMaximum number of connections to open to the SQL server at any 1 timeNo5
connection_timeoutNumberNumber of milliseconds before a SQL connection is closedNo10000
flush_sizeNumberMaximum number of entries to buffer before sending to SQL - if this is reached before idle_flush_timeNo1000
max_flush_exceptionsNumberNumber of sequential flushes which cause an exception, before the set of events are discarded. Set to a value less than 1 if you never want it to stop. This should be carefully configured with respect to retry_initial_interval and retry_max_interval, if your SQL server is not highly availableNo10
retry_initial_intervalNumberNumber of seconds before the initial retry in the event of a failure. On each failure it will be doubled until it reaches retry_max_intervalNo2
retry_max_intervalNumberMaximum number of seconds between each retryNo128
retry_sql_statesArray of stringsAn array of custom SQL state codes you wish to retry until max_flush_exceptions. Useful if you're using a JDBC driver which returns retry-able, but non-standard SQL state codes in it's exceptions.No[]
event_as_json_keywordStringThe magic key word that the plugin looks for to convert the entire event into a JSON object. As Logstash does not support this out of the box with it's sprintf implementation, you can use whatever this field is set to in the statement parametersNo@event
enable_event_as_json_keywordBooleanEnables the magic keyword set in the configuration option event_as_json_keyword. Without this enabled the plugin will not convert the event_as_json_keyword into JSON encoding of the entire event.NoFalse

Example configurations

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拼命小孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值