前言
昨天在本地装好了ELK的环境,今天准备将mysql的数据用logstash导入Elasticsearch,然后在springboot项目中实现博客的检索功能。
安装logstash-input-jdbc
要想导入Mysql的数据到Elasticsearch,需要安装插件——logstash-input-jdbc。
Elasticsearch、logstash下载地址:https://www.elastic.co/downloads
插件github地址:logstash-plugins/logstash-input-jdbc
安装此插件以及依赖需要在ruby环境下,ruby默认的镜像在国外,所以很慢,需要手动将镜像网站https://rubygems.org/替换为国内的https://gems.ruby-china.com/
具体步骤如下:(MacOS系统)
1.1安装gem
gem -v
gem update --system
1.2给gem更换网站源
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem sources -l
1.3
修改Gemfile
cd /usr/local/logstash-6.4.2
vim Gemfile
#修改第1行出现的source,将source "https://rubygems.org",改为source "https://gems.ruby-china.com/"
vim Gemfile.lock
#修改GEM下的remote,将remote: https://rubygems.org,改为remote: https://gems.ruby-china.com/
1.4安装logstash-input-jdbc插件
执行完上面3步后,终于可以安装插件了,启动logstash的bin目录下的logstash-plugin命令,执行安装。
bin/logstash-plugin install logstash-input-jdbc
安装完成不报错就应该是成功了,可用bin/logstash-plugin list查看所有已安装插件,加上–verbose参数可以显示插件版本号。
bin/logstash-plugin list
bin/logstash-plugin list --verbose
配置logstash的conf文件
在bin文件夹中新建pipelines文件夹
里面新建article.conf文件,然后加入
input {
jdbc {
#jar包可以存放在任意路径
jdbc_driver_library => "/usr/local/Cellar/logstash/7.6.2/mysql-connector-java-8.0.19.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "mysql地址"
jdbc_user => "用户名"
jdbc_password => "密码"
#schedule设置每分钟执行
schedule => "* * * * *"
statement => "SELECT id ,author, title , verbal_content as content FROM article "
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
#index表示存入elasticsearch中的索引名字
index => "articles"
#document_id主码
document_id => "%{id}"
}
stdout { codec => json_lines }
}
最后发现,需要加入对应版本的mysql-jdbc的jar报,才能完成数据的导入
运行过程及结果
在bin目录运行
./logstash -f pipelines/article.conf
最终的抽取结果