MySQL数据以全量和增量方式,同步到ES搜索引擎

Wesley13
• 阅读 499

本文源码:GitHub·点这里 || GitEE·点这里

一、配置详解

场景描述:MySQL数据表以全量和增量的方式向ElasticSearch搜索引擎同步。

1、下载内容

  • elasticsearch 版本 6.3.2
  • logstash 版本 6.3.2
  • mysql-connector-java-5.1.13.jar

2、核心配置

  • 路径:/usr/local/logstash
  • 新建配置目录:sync-config

1)、配置全文

/usr/local/logstash/sync-config/cicadaes.conf

input {
    stdin {}
    jdbc {
        jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/cicada?characterEncoding=utf8"
        jdbc_user => "root"
        jdbc_password => "root123"
        jdbc_driver_library => "/usr/local/logstash/sync-config/mysql-connector-java-5.1.13.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_paging_enabled => "true"
        jdbc_page_size => "50000"
        jdbc_default_timezone => "Asia/Shanghai"
        statement_filepath => "/usr/local/logstash/sync-config/user_sql.sql"
        schedule => "* * * * *"
        type => "User"
        lowercase_column_names => false
        record_last_run => true
        use_column_value => true
        tracking_column => "updateTime"
        tracking_column_type => "timestamp"
        last_run_metadata_path => "/usr/local/logstash/sync-config/user_last_time"
        clean_run => false
    }
    jdbc {
        jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/cicada?characterEncoding=utf8"
        jdbc_user => "root"
        jdbc_password => "root123"
        jdbc_driver_library => "/usr/local/logstash/sync-config/mysql-connector-java-5.1.13.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_paging_enabled => "true"
        jdbc_page_size => "50000"
        jdbc_default_timezone => "Asia/Shanghai"
        statement_filepath => "/usr/local/logstash/sync-config/log_sql.sql"
        schedule => "* * * * *"
        type => "Log"
        lowercase_column_names => false
        record_last_run => true
        use_column_value => true
        tracking_column => "updateTime"
        tracking_column_type => "timestamp"
        last_run_metadata_path => "/usr/local/logstash/sync-config/log_last_time"
        clean_run => false
    }
}
filter {
    json {
        source => "message"
        remove_field => ["message"]
    }
}
output {
    if [type] == "User" {
        elasticsearch {
            hosts => ["127.0.0.1:9200"]
            index => "cicada_user_search"
            document_type => "user_search_index"
        }
    }
    if [type] == "Log" {
        elasticsearch {
            hosts => ["127.0.0.1:9200"]
            index => "cicada_log_search"
            document_type => "log_search_index"
        }
    }
}

2)、SQL文件

  • user_sql.sql

    SELECT id, user_name userName, user_phone userPhone, create_time createTime, update_time updateTime FROM c_user WHERE update_time > : sql_last_value

  • log_sql.sql

    SELECT id, param_value paramValue, request_ip requestIp, create_time createTime, update_time updateTime FROM c_log WHERE update_time > : sql_last_value

3)、配置参数说明

  • input参数

    statement_filepath:读取SQL语句位置 schedule :这里配置每分钟执行一次 type :类型,写入ES的标识 lowercase_column_names :字段是否转小写 record_last_run :记录上次执行时间 use_column_value :使用列的值 tracking_column :根据写入ES的updateTime字段区分增量数据 tracking_column_type :区分的字段类型

  • output参数

    hosts :ES服务地址 index :Index名称,类比理解数据库名称 document_type :Type名称,类比理解表名称

3、启动进程

/usr/local/logstash/bin/logstash  
-f  
/usr/local/logstash/sync-config/cicadaes.conf

二、ES客户端工具

1、下载软件

kibana-6.3.2-windows-x86_64

2、修改配置

kibana-6.3.2-windows-x86_64\config\kibana.yml

添加配置:

elasticsearch.url: "http://127.0.0.1:9200"

3、双击启动

kibana-6.3.2-windows-x86_64\bin\kibana.bat

4、访问地址

http://localhost:5601

MySQL数据以全量和增量方式,同步到ES搜索引擎

三、源代码地址

GitHub·地址
https://github.com/cicadasmile/linux-system-base
GitEE·地址
https://gitee.com/cicadasmile/linux-system-base

MySQL数据以全量和增量方式,同步到ES搜索引擎

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
2年前
Linux系统:Centos7下搭建ClickHouse列式存储数据库
本文源码:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Flinuxsystembase)||GitEE·点这里(https://gitee.com/cicadasmile/linuxsystem
Stella981 Stella981
2年前
Linux系统:centos7下搭建Rocketmq4.3中间件,配置监控台
本文源码:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Flinuxsystembase)||GitEE·点这里(https://gitee.com/cicadasmile/linuxsystem
Stella981 Stella981
2年前
Linux系统:Centos7环境搭建Redis单台和集群环境
本文源码:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Flinuxsystembase)||GitEE·点这里(https://gitee.com/cicadasmile/linuxsystem
Stella981 Stella981
2年前
Linux系统:centos7下搭建Nginx和FastDFS文件管理中间件
本文源码:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Flinuxsystembase)||GitEE·点这里(https://gitee.com/cicadasmile/linuxsystem
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这