ES用logstash导入数据库,用其他字段做_id主键

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}”

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值