ES版本: 6.2.1 logstash版本 6.2.1
目的:一次偶然在把mysql数据同步到ES,同步时在不经意间在mysql修改了几数据信息,导致在同步的过程中,在ES中加入大量重复数据,但是因为主键是自动生成,所以存在主键不同,但是数据相同的数据,在项目展示测试中有很多相同数据。 解决方法之一就是把sql主键也应用到ES,即使重复添加,也只是报错,而不是加入大量重复数据。
方式:配置了sql.config,开启了模板:
----------------------------------------------模板---------------------------------------------------------
{
"mappings" : {
"course" : {
"_source" : {
"excludes" : [ "teachplan", "description", "content" ]
},
"properties" : {
"content" : {
"analyzer" : "ik_max_word",
"type" : "text"
},
--------有很多配置,自行根据业务需要配置--------
"end_time" : {
"type" : "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
},
"template" : "course"
}
------------------------------------------------sql配置---------------------------------------------------
input {
stdin {
}
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/yh_course?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC"
# the user we wish to excute our statement as
jdbc_user => "root"
jdbc_password => root
# the path to our downloaded jdbc driver
jdbc_driver_library => "D:/repository/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar"
# the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
#要执行的sql文件
#statement_filepath => "/conf/course.sql"
statement => "select * from course_pub where timestamp > date_add(:sql_last_value,INTERVAL 8 HOUR)"
#定时配置
schedule => "* * * * *"
record_last_run => true
last_run_metadata_path => "E:/ES/elasticsearch/logstash-6.2.1/config/logstash_metadata"
}
}
output {
elasticsearch {
#ES的ip地址和端口
hosts => "localhost:9200"
#hosts => ["localhost:9200","localhost:9202","localhost:9203"]
#ES索引库名称
index => "yh_course5"
document_type => "doc"
template =>"E:/ES/elasticsearch/logstash-6.2.1/config/yh_course_template.json"
template_name =>"yh_course5"
document_id => "%{id}" 《----------------------------在这儿
template_overwrite =>"true"
}
stdout {
#日志输出
codec => json_lines
}
}
全文的精华就是想突出 document_id => “%{id}” 这里把我的主键替换为sql数据库中的“id”字段。可是更换成其他的名字,比如说你的sql数据库主键是uuid 就可以写成 document_id => “%{uuid}”