logstash6.5.4同步mysql数据到elasticsearch 6.4.1

下载logstash-6.5.4 ZIP解压和es 放到es根目录下

下载mysql jdbc的驱动 mysql-connector-java-8.0.12 放在任意目录下

以下方式采用动态模板,还有一种方式静态模板可以指定字段映射,那样效率低一点。

一、创建配置文件 mysqltoes.conf  

input {
stdin { }
jdbc {
#填写你的mysql链接串8以后驱动必须这样写,不然后出错,这个问题我搞了好几天才解决 {host}:3306/{database}
jdbc_connection_string => "jdbc:mysql://localhost:3306/easycms?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"
#链接数据库用户名称
jdbc_user => "root"
#链接数据库的密码
jdbc_password => "root"
#指定驱动的位置
jdbc_driver_library => "D:/soft/elasticsearch-6.4.1/bin/mysql-connector-java-8.0.12.jar"
#最新的mysql驱动写法,写以前的驱动会报错
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
#同步的表,这里也可以只想一个写了sql的文件
statement => "SELECT * from sys_message"
#表示每分钟都同步数据
schedule => "* * * * *"
}
}
filter {
date {
# 有多个项的话能匹配多个不同的格式
match => [ "createAt", "MMM dd yyyy HH:mm:ss","ISO8601" ]
target => "fieldName1"
timezone => "Asia/Shanghai"
}
}

output {
stdout {
codec => json_lines
}
elasticsearch {
#数据到es
hosts => "localhost:9200"
#指定索引,名字任意
index => "sysmessage"
#指定类型,任意
document_type => "messagedata"
document_id => "%{id}"
template_overwrite => true
#模板中的template名需要和output中的索引名一致,template可以使用*配置任意字符。
template => "mysqltoes-template.json"
}
}

二、创建mysqltoes-template.json 也放在logstash-6.5.4in目录下

      使用logstash dynamic template功能,字段添加映射ik分词器

{
"template":"*",
"version":60001,
"settings":{
"index.refresh_interval":"5s"
},
"mappings":{
"_default_":{
"dynamic_templates":[
{
"message_field":{
"path_match":"message",
"match_mapping_type":"string",
"mapping":{
"type":"text",
"norms":false
}
}
},
{
"string_fields":{
"match":"*",
"match_mapping_type":"string",
"mapping":{
"type":"text",
"norms":false,
"analyzer":"ik_max_word",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
}
}
}
],
"properties":{
"timestamp":{
"type":"date"
},
"version":{
"type":"keyword"
},
"geoip":{
"dynamic":true,
"properties":{
"ip":{
"type":"ip"
},
"location":{
"type":"geo_point"
},
"latitude":{
"type":"half_float"
},
"longitude":{
"type":"half_float"
}
}
}
}
}
}
}

三、执行命令

     然后切换到logstash-6.5.4in 目录下执行命令  logstash -f mysqltoes.conf

四、效果

参考:https://blog.csdn.net/u012976879/article/details/85259911

https://www.cnblogs.com/jstarseven/p/7707499.html

https://blog.csdn.net/github_38787002/article/details/84993664

原文地址:https://www.cnblogs.com/langhaoabcd/p/10289005.html