说明:该文档只是单表同步,多表同步的请继续百度,还有我的es没有集群,单节点的
一、安装elasticsearch6.5.1 (怎么安装请百度)
二、安装logstash-6.5.1(怎么安装请百度)
三、重点:配置logstash.conf文件
input {
stdin { }
jdbc {
#填写你的mysql链接串8以后驱动必须这样写{host}:3306/{database}
jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/数据库名称?characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true"
#链接数据库用户名称
jdbc_user => "root"
#链接数据库的密码
jdbc_password => "root"
#指定驱动的位置
jdbc_driver_library => "/data/es/mysql-connector-java-5.1.47.jar"
#最新的mysql驱动写法,写以前的驱动会报错
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_validate_connection => "true"
jdbc_paging_enabled => true
jdbc_page_size => "1000"
jdbc_default_timezone =>"Asia/Shanghai"
#同步的表,这里也可以只想一个写了sql的文件
statement => "select * from 表名 where update_time >= :sql_last_value order by update_time desc"
#表示每分钟都同步数据
schedule => "* * * * *"
record_last_run => true
use_column_value => true
tracking_column_type => "numeric"
tracking_column => "update_time"
last_run_metadata_path => "/data/es/logstash-6.5.1/logs/last_id.txt"
clean_run => false
}
}
filter {
json { source => "message" remove_field => ["message"] }
#date {
# 有多个项的话能匹配多个不同的格式
#match => ["create_time", "yyyy-MM-dd HH:mm:ss"]
#target => "fieldName1"
#timezone => "Asia/Shanghai"
#}
#下面:当使用Logstash自动生成的mapping模板时过滤掉@timestamp和@version字段
mutate {
remove_field => ["@timestamp","@version"]
}
}
output {
stdout {
codec => json_lines
}
elasticsearch {
#数据到es
hosts => "localhost:es端口"
#指定索引,名字任意
index => ""
#指定类型,任意
document_type => ""
document_id => "%{id}"
#template_overwrite => true
#模板中的template名需要和output中的索引名一致,template可以使用*配置任意字符。
#template => "es模板json文件"
}
}
last_id.txt 文件 在启动前必须手动创建,不然会报错,last_txt文件找不到,文件是记录的最后一次 update_time 时间戳。
四、es模板json文件根据自己mysq表的结构去编辑不要嫌麻烦,编辑好,先去es-head验证json是否ok,不然会报错。
模板样例:
{
"template": "*",
"version": 60001,
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0,
"index.refresh_interval": "5s"
},
"mappings": {
"_default_": {
"_source": {
"enabled": true
},
"properties": {
"id": {
"type": "integer"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
等等字段
}
}
}
}
启动:进入bin目录 执行 ./logstash -f logstash.conf 如有其它报错请自行百度,或留言。
测试,update 表名 set update_time=time(),title=132456 where id=123; 执行后,logstash会自动把mysql更新的数据更新到es上。
logs 文件夹下 可以查看 logstash-plain.log 文件,里面记录的是执行sql语句记录。
到此完毕。
有不足的地方,欢迎留言指正。。。