以Windows操作系统为例
1.下载logstash安装包,解压,配置好环境
2.在config目录中新增一个jdbc.conf
input { stdin { } jdbc { # mysql 数据库链接,es为数据库名 jdbc_connection_string => "jdbc:mysql://localhost:3306/es?characterEncoding=utf8&useSSL=true&serverTimezone=GMT" # 用户名和密码 jdbc_user => "root" jdbc_password => "root" # 驱动 jdbc_driver_library => "C:/Users/Administrator/.m2/repository/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar" # 驱动类名 jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_paging_enabled => "true" jdbc_page_size => "50000" # 执行的sql 文件路径+名称 statement_filepath => "H:/software/java/logstash-6.2.2/config/test.sql" # 设置监听间隔 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新 schedule => "* * * * *" # 设定ES索引类型 type => "content" }}filter { json { source => "message" remove_field => ["message"] } } output { elasticsearch { hosts => ["localhost:9200"] #ES索引名称(自己定义的) index => "cmscontent" #自增ID编号 document_id => "%{id}" } stdout { codec => json_lines }}
3.在config目录中新增一个test.sql 内容为:select * from test
4.cmd在bin目录下执行logstash -f ../config/jdbc.conf