去elastic官网下载logstash(logstash的版本根据elasticsearch的版本来下载),下载解压之后在logstash-6.3.2的config目录下添加mysql.conf文件
input{
jdbc{
#jdbc驱动包设置
jdbc_driver_library => "D:\\tool\\logstash-6.3.2\\logstash-6.3.2\\mysql-connector-java-5.1.13-bin.jar"
#mysql驱动包
jdbc_driver_class => "com.mysql.jdbc.Driver"
#mysql数据库连接信息
jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/class"
#mysql用户名
jdbc_user => "root"
#密码
jdbc_password => "root"
#定时任务
schedule => "* * * * *"
#清空上一次数据
clean_run => true
#要执行的sql
statement => "select * from student where update_time > :sql_last_value and update_time < NOW() ORDER BY update_time DESC"
}
}
output{
elasticsearch{
#es host : port
hosts => ["127.0.0.1:9200"]
#索引
index => "class"
#id为数据库的id
document_id => "%{id}"
}
}
数据库表结构如下
完成以上工作后进入logstash-6.3.2的bin目录下打开dos窗口执行:logstash -f …/config/mysql.conf即可进行数据同步。