1、应用背景
咱们习惯性,将数据存储到mysql,那么怎么应用ES实现全文检索呢。Elasticsearch为咱们提供了一个插件logstash来同步咱们的数据库。
2、装置logsash插件
注意事项:要装置跟es版本一样的版本。
logstash下载地址:https://www.elastic.co/downloads/logstash
在windows上面解压即可应用。我这里用的都是7.2.0版本。不须要再像2.x版本须要集成logstash-jdbc-input,能力实现数据同步。
3、配置logsash实现同步
1)在根目录下新建文件夹mysqletc,用来搁置同步所须要的文件和mysql驱动
文件夹内容:
2)将要同步的数据库对应的mysql驱动放到该文件夹
3)在创立的新文件夹中(mysqletc文件夹)中创立sql文件
sql文件里就是你须要同步到es中的数据的查问语句,如果是全量同步,只须要要select * from [table]即可。
如果同步多个表格能够创立多个sql文件。
比方我的:
soul.sql
SELECT * FROM soul
mto_user.sql
SELECT * FROM mto_user
留神:结尾不要加分号,我加分号同步报错了
3)logstash链接数据库和Elasticsearch的conf文件
名字轻易取,我这里习惯叫mysql.conf,内容如下:
input {
stdin {}
jdbc {
# mysql 数据库链接,shop为数据库名
jdbc_connection_string => "jdbc:mysql://114.67.169.20:3306/myblog"
# 用户名和明码
jdbc_user => "root"
jdbc_password => "Wylc!@#$5"
# 驱动
jdbc_driver_library => "C:/softs/elk/logstash-7.2.0/mysqletc/mysql-connector-java-8.0.13.jar"
# 驱动类名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 执行的sql 文件门路+名称
statement_filepath => "C:/softs/elk/logstash-7.2.0/mysqletc/soul.sql"
# 设置监听距离 各字段含意(由左至右)分、时、天、月、年,全副为*默认含意为每分钟都更新
schedule => "* * * * *"
# 索引类型
type => "soul"
}
jdbc {
# mysql 数据库链接,shop为数据库名
jdbc_connection_string => "jdbc:mysql://114.67.169.20:3306/myblog"
# 用户名和明码
jdbc_user => "root"
jdbc_password => "Wylc!@#$5"
# 驱动
jdbc_driver_library => "C:/softs/elk/logstash-7.2.0/mysqletc/mysql-connector-java-8.0.13.jar"
# 驱动类名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 执行的sql 文件门路+名称
statement_filepath => "C:/softs/elk/logstash-7.2.0/mysqletc/mto_user.sql"
# 设置监听距离 各字段含意(由左至右)分、时、天、月、年,全副为*默认含意为每分钟都更新
schedule => "* * * * *"
# 索引类型
type => "mto_user"
}
}
filter {
json {
source => "message"
remove_field => ["message"]
}
}
output {
if [type]=="soul"{
elasticsearch {
hosts => ["localhost:9200"]
index => "soul"
document_id => "%{id}"
}
}
if [type]=="mto_user"{
elasticsearch {
hosts => ["localhost:9200"]
index => "mto_user"
document_id => "%{id}"
}
}
stdout {
codec => json_lines
}
}
留神:创立的所有文本文件肯定要是utf-8无BOM格局编码
实现了下面的步骤,logstash的配置也就实现了。
4、启动logstash开始同步数据库
第一步:运行elasticsearch.bat文件,关上elasticsearch,运行胜利能够看到:
第二步:到bin目录下创立一个批处理文件run_default.bat,内容为:
logstash -f ../mysqletc/mysql.conf
第三步:双击run_default.bat,其中logstash -f 示意运行指令, ../mysqlec/mysql.conf示意咱们配置的mysql.conf文件门路,胜利启动后,能够在终端中看见运行的sql和同步的数据
5、测试是否同步胜利
同步实现后,咱们能够去head插件里,查看es中是否存在咱们方才创立的索引库:
在根本查问中,能够看见咱们同步的数据
其中:timestamp和version是elastisearch本人增加的字段。
相干文章:怎么装置应用elasticsearch-head插件
input {
stdin {}
jdbc {
# mysql 数据库链接,shop为数据库名
jdbc_connection_string => "jdbc:mysql://114.67.169.20:3306/myblog"
# 用户名和明码
jdbc_user => "root"
jdbc_password => "xxxxxx"
# 驱动
jdbc_driver_library => "C:/softs/elk/logstash-7.2.0/mysqletc/mysql-connector-java-8.0.13.jar"
# 驱动类名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 执行的sql 文件门路+名称
statement_filepath => "C:/softs/elk/logstash-7.2.0/mysqletc/soul.sql"
# 设置监听距离 各字段含意(由左至右)分、时、天、月、年,全副为*默认含意为每分钟都更新
schedule => "* * * * *"
# 索引类型
type => "soul"
}
jdbc {
# mysql 数据库链接,shop为数据库名
jdbc_connection_string => "jdbc:mysql://114.67.169.20:3306/myblog"
# 用户名和明码
jdbc_user => "root"
jdbc_password => "xxxxxx"
# 驱动
jdbc_driver_library => "C:/softs/elk/logstash-7.2.0/mysqletc/mysql-connector-java-8.0.13.jar"
# 驱动类名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 执行的sql 文件门路+名称
statement_filepath => "C:/softs/elk/logstash-7.2.0/mysqletc/mto_user.sql"
# 设置监听距离 各字段含意(由左至右)分、时、天、月、年,全副为*默认含意为每分钟都更新
schedule => "* * * * *"
# 索引类型
type => "mto_user"
}
}
filter {
json {
source => "message"
remove_field => ["message"]
}
}
output {
if [type]=="soul"{
elasticsearch {
hosts => ["localhost:9200"]
index => "soul"
document_id => "%{id}"
}
}
if [type]=="mto_user"{
elasticsearch {
hosts => ["localhost:9200"]
index => "mto_user"
document_id => "%{id}"
}
}
stdout {
codec => json_lines
}
}
相干文章:
发表回复