go-mysql-elasticsearch实现mysql 与elasticsearch实时同步深入详解

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 本文深入详解了插件的安装、使用、增删改查同步测试。

引言:

go-mysql-elasticsearch 是国内作者开发的一款插件。测试表明:该插件优点:能实现同步增、删、改、查操作。不足之处(待完善的地方):
1、仍处理开发、相对不稳定阶段;
2、没有日志,不便于排查问题及查看同步结果。
本文深入详解了插件的安装、使用、增删改查同步测试。

1、 go-mysql-elasticsearch 插件安装

步骤1:安装go

yum install go

步骤2:安装godep

go get github.com/tools/godep

步骤3:获取go-mysql-elastisearch插件

go get github.com/siddontang/go-mysql-elasticsearch

步骤4:安装go-mysql-elastisearch插件

cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch
make

2、go-mysql-elasticsearch 插件使用

2.1 修改配置文件

[root@5b9dbaaa148a etc]# cat river.toml
# MySQL address, user and password
# user must have replication privilege in MySQL.
my_addr = "192.168.1.1:3306"
my_user = "root"
my_pass = "password@!"

# Elasticsearch address
es_addr = "192.168.1.1:9200"

# Path to store data, like master.info, and dump MySQL data 
data_dir = "./var"

# Inner Http status address
stat_addr = "192.168.1.1:12800"

# pseudo server id like a slave 
server_id = 1

# mysql or mariadb
flavor = "mysql"

# mysqldump execution path
mysqldump = "mysqldump"

# MySQL data source
[[source]]
schema = "test"

# Only below tables will be synced into Elasticsearch.
# "test_river_[0-9]{4}" is a wildcard table format, you can use it if you have many sub tables, like table_0000 - table_1023
# I don't think it is necessary to sync all tables in a database.
tables = ["cc"]

# Below is for special rule mapping
#[[rule]]
#schema = "test"
#table = "cc"
#index = "go_river"
#type = "go_rivert"

    # title is MySQL test_river field name, es_title is the customized name in Elasticsearch
 #   [rule.field]
    # This will map column title to elastic search my_title
  #  title="es_title"
    # This will map column tags to elastic search my_tags and use array type
   # tags="my_tags,list"
    # This will map column keywords to elastic search keywords and use array type
    #keywords=",list"

# wildcard table rule, the wildcard table must be in source tables 
[[rule]]
schema = "test"
table = "cc"
index = "gocc"
type = "gocc_t"

    # title is MySQL test_river field name, es_title is the customized name in Elasticsearch
    [[rule.fields]]
    mysql = "mysql101"
    elastic = "es_mysql101"
AI 代码解读

2.2 执行同步操作

cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch 
./bin/go-mysql-elasticsearch -config=./etc/river.toml
AI 代码解读

3、 go-mysql-elasticsearch 插件同步测试结果

3.1 插入Insert操作实时同步验证(验证ok)

3.1.1 Mysql端插入操作

mysql> insert into cc(id,name) values(12, ‘test12’); 
Query OK, 1 row affected (0.06 sec)
AI 代码解读

3.1.2Mysql执行insert后查询结果

mysql> select * from cc where id =12; 
+—-+——–+——–+———————+ 
| id | name | status | modified_at | 
+—-+——–+——–+———————+ 
| 12 | test12 | ok | 2016-06-24 02:27:29 | 
+—-+——–+——–+———————+ 
1 row in set (0.02 sec)
AI 代码解读

3.1.3 ES端能查询到新增的value字段。

[root@5b9dbaaa148a bin]# curl -XGET http://192.168.1.1:9200/gocc/_search?pretty -d '
> {"query":
> {"term":
> {"id":12}}}'
{
  "took" : 402,
  "timed_out" : false,
  "_shards" : {
    "total" : 8,
    "successful" : 8,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "gocc",
      "_type" : "gocc_t",
      "_id" : "12",
      "_score" : 1.0,
      "_source" : {
        "id" : 12,
        "modified_at" : "2016-06-24T02:27:29+01:00",
        "name" : "test12",
        "status" : "ok"
      }
    } ]
  }
}
AI 代码解读

3.2 修改Update操作实时同步验证(验证ok)

3.2.1 mysql执行更新操作

mysql> update cc set name = 'test12_001' where id = 12;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0
AI 代码解读

3.2.2 mysql执行修改后查询

Mysql查询修改后结果:

mysql> select * from cc where id = 12;
+----+------------+--------+---------------------+
| id | name       | status | modified_at         |
+----+------------+--------+---------------------+
| 12 | test12_001 | ok     | 2016-06-24 02:27:29 |
+----+------------+--------+---------------------+
1 row in set (0.00 sec)
AI 代码解读

3.2.3 ES查询修改结果

[root@5b9dbaaa148a bin]# curl -XGET http://192.168.1.1:9200/gocc/_search?pretty -d '
{"query":
{"term":
{"id":12}}}'
{
  "took" : 59,
  "timed_out" : false,
  "_shards" : {
    "total" : 8,
    "successful" : 8,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "gocc",
      "_type" : "gocc_t",
      "_id" : "12",
      "_score" : 1.0,
      "_source" : {
        "id" : 12,
        "modified_at" : "2016-06-24T02:27:29+01:00",
        "name" : "test12_001",
        "status" : "ok"
      }
    } ]
  }
}
AI 代码解读

3.3 删除操作实时同步验证

3.3.1 Mysql执行删除操作

mysql> delete from cc where id = 12;
Query OK, 1 row affected (0.04 sec)
AI 代码解读

3.3.2 删除后查询表

mysql> select * from cc;
+----+--------------------+--------+---------------------+
| id | name               | status | modified_at         |
+----+--------------------+--------+---------------------+
|  1 | laoyang360         | ok     | 0000-00-00 00:00:00 |
|  2 | test002            | ok     | 2016-06-23 06:16:42 |
|  3 | dlllaoyang360      | ok     | 0000-00-00 00:00:00 |
| 11 | test11             | ok     | 2016-06-24 02:09:15 |
|  5 | jdbc_test_update08 | ok     | 0000-00-00 00:00:00 |
|  7 | test7              | ok     | 0000-00-00 00:00:00 |
|  8 | test008            | ok     | 0000-00-00 00:00:00 |
|  9 | test009            | ok     | 0000-00-00 00:00:00 |
| 10 | test10             | ok     | 2016-06-24 02:08:14 |
+----+--------------------+--------+---------------------+
9 rows in set (0.02 sec)
AI 代码解读

3.3.3 ES查询删除后结果

[root@5b9dbaaa148a bin]# curl -XGET http://192.168.1.1:9200/gocc/_search?pretty -d '
{"query":
{"term":
{"id":12}}}'
{
  "took" : 40,
  "timed_out" : false,
  "_shards" : {
    "total" : 8,
    "successful" : 8,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}
AI 代码解读

4、小结

验证发现:
(1)go-mysql-elasticsearch插件可以实现同步insert、update、delete操作。
(2)可视化做的不好,没有打印日志。
(3)go-mysql-elasticsearch尚不大稳定,出现过无法同步成功的情况,但没有报错。不便于排查。


作者:铭毅天下
转载请标明出处,原文地址:http://blog.csdn.net/laoyang360/article/details/51771483

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
打赏
0
0
0
0
225
分享
相关文章
MySQL 历史数据迁移到 Elasticsearch
MySQL 历史数据迁移到 Elasticsearch
207 4
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
MySQL数据实时同步到Elasticsearch:技术深度解析与实践分享
在当今的数据驱动时代,实时数据同步成为许多应用系统的核心需求之一。MySQL作为关系型数据库的代表,以其强大的事务处理能力和数据完整性保障,广泛应用于各种业务场景中。然而,随着数据量的增长和查询复杂度的提升,单一依赖MySQL进行高效的数据检索和分析变得日益困难。这时,Elasticsearch(简称ES)以其卓越的搜索性能、灵活的数据模式以及强大的可扩展性,成为处理复杂查询需求的理想选择。本文将深入探讨MySQL数据实时同步到Elasticsearch的技术实现与最佳实践。
361 0
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
134 5
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
485 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
这篇文章提供了通过Docker安装Elasticsearch和Kibana的详细过程和图解,包括下载镜像、创建和启动容器、处理可能遇到的启动失败情况(如权限不足和配置文件错误)、测试Elasticsearch和Kibana的连接,以及解决空间不足的问题。文章还特别指出了配置文件中空格的重要性以及环境变量中字母大小写的问题。
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
8月前
|
docker desktop安装es并连接elasticsearch-head:5
以上就是在Docker Desktop上安装Elasticsearch并连接Elasticsearch-head:5的步骤。
324 2
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等