logstash 版本:7.5
配置logstash-sample.conf
input {
jdbc {
jdbc_driver_library => "./logstash-7.5.0/logstash-core/lib/jars/mysql-connector-java-5.1.36.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_default_timezone =>"Asia/Shanghai"
jdbc_connection_string => "jdbc:mysql://host:port/mydb?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai"
jdbc_user => "root"
jdbc_password => "password"
statement => "select * from big_data where id > :sql_last_value limit 2000"
use_column_value => true
tracking_column => "id"
clean_run => true
schedule => "* * * * * *"
}
}
output {
elasticsearch {
hosts => "http://127.0.0.1:9200"
index => "test_index"
document_type => "test_type"
document_id => "%{id}"
}
}
啓動命令 .\bin\logstash.bat -f .\config\logstash-sample.conf
問題解決
類似 jdbc_driver_class not fount 的問題
注意包的引用路徑,需要放到:
./logstash-7.5.0/logstash-core/lib/jars
目錄下
sql_last_value
使用問題
這個問題找了很久的答案,默認情況下該參數爲上一次同步的時間點,這種在同步增量數據的時候還是可以使用的,但是如果想要同步全量數據那麼更希望sql_last_value
是上一次同步的最後一條記錄的ID,查閱官方文檔:
-
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 ifuse_column_value
is true andtracking_column
is set. It is updated accordingly after subsequent queries are run. -
如果要根據時間點即默認的
sql_last_value
來同步增量數據卻發現時間不對,則需要通過jdbc_default_timezone =>"Asia/Shanghai"
處理。 -
如果全量需要將
sql_last_value
初始化則使用clean_run => true
官方文檔:https://www.elastic.co/guide/en/logstash/7.5/plugins-inputs-jdbc.html