使用情況說明:
已經使用logstash拉取MySQL數據存儲到es中,es中也創建了相應的索引,也存儲了數據。假若把這個索引給刪除了,再次進行同步操作的話要咋做,從最開始的數據進行同步,而不是新增的數據
官方原話:
The plugin will persist the sql_last_value parameter in the form of a metadata 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 time the pipeline starts up, this value will be updated by reading from the file. If clean_run is set to true, this value will be ignored and sql_last_value will be set to Jan 1, 1970, or 0 if use_column_value is true, as if no query has ever been executed.
翻譯:
插件將以sql_last_value元數據文件的形式保存配置文件中的參數last_run_metadata_path。執行查詢后,該文件將更新為的當前值sql_last_value。下次管道啟動時,將通過從文件中讀取來更新此值。如果 clean_run設置為true,則將忽略此值並將其sql_last_value設置為1970年1月1日;如果use_column_value為true,則將其設置為0 ,就好像從未執行過任何查詢一樣。
具體到操作:
jdbc {
jdbc_connection_string => "jdbc:mysql://192.168.0.145:3306/db_example?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"
jdbc_user => "root"
jdbc_password => "root"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_driver_library => ""
jdbc_paging_enabled => true
tracking_column => "unix_ts_in_secs"
use_column_value => true
tracking_column_type => "numeric"
schedule => "*/5 * * * * *"
statement => "SELECT *, UNIX_TIMESTAMP(modification_time) AS unix_ts_in_secs FROM es_table WHERE (UNIX_TIMESTAMP(modification_time) > :sql_last_value AND modification_time < NOW()) ORDER BY modification_time ASC"
}
通過查看jdbc{}語句中的statement,可以知道監控的是unix_ts_in_secs字段值,其值是UNIX_TIMESTAMP(modification_time)過來的
參數last_run_metadata_path默認會從$HOME/.logstash_jdbc_last_run
文件中獲取最后一次的值,也就是說,數據表中監控的modification_time字段數值,比文件中存儲的大,則會拉取數據,否則就不會。
具體到我這邊,這個文件的路徑是/root/.logstash_jdbc_last_run
[root@bogon ~]# cat /root/.logstash_jdbc_last_run
--- 1589189560
里面存儲的是unix時間戳,跟數據表中最后一條數據的modification_time字段值想匹配。
那么接下來,或者修改這個文件里的時間戳值為數據表中modification_time字段最早的那個值,或者刪除這個文件,然后再次執行拉取命令即可實現想要的再次同步數據到es的效果。