go-mysql-transfer 同步数据到es

同步数据需要注意的事项 前提条件

1 要同步的mysql 表必须包含主键

2 mysql binlog 必须是row 模式

3 不支持程序运行过程中修改表结构

4 要赋予连接mysql 账号的权限 reload, replication super 权限 如果是root 权限则不需要

安装 go-mysql-transfer

复制代码
​
git clone  https://github.com/wj596/go-mysql-transfer.git

cd go-mysql-transfer
​go get
go build

1 设置mysql binlog模式为row

同步数据位置 查看mysql binlog 日志位置

确保binlog 日志为 on

数据库结构 表 test

app.yml 配置

复制代码
# mysql
addr: 192.168.217.134:3306
user: root
pass: Devops%8
charset : utf8
slave_id: 1001 #slave ID
flavor: mysql #mysql or mariadb,榛???mysql

target: elasticsearch #

#elasticsearch
es_addrs: 127.0.0.1:9200
es_version: 6 # 
#es_password:  #
#es_version:  #

rule:
  - schema: game
    table: lottery 
    order_by_column: id
    column_underscore_to_camel: false 
    value_encoder: json
    
    es_index: lottery 
    es_mappings: 
      - column: id
        field: id
        type: long
      - column: lose
        field: lose #es
        type: keyword
      - column: win
        field: win
        type: keyword
      - column: stake
        field: stake
        type: double
      - column: odds
        field: odds
        type: double

全局数据初始化

复制代码
./go-mysql-transfer -stock

增量数据同步

修改mysql 的个别数据 同步binlog到最新位置 。。。执行同步操作

同步数据到最新位置,修改部分数据。。。

./go-mysql-transfer -position binlog.000015 2457

复制代码
[root@localhost data]# ./go-mysql-transfer -position binlog.000015 2457 
2024-04-27 10:14:36.564695 I | process id: 46615
2024-04-27 10:14:36.564905 I | GOMAXPROCS :2
2024-04-27 10:14:36.564909 I | source  mysql(192.168.217.134:3306)
2024-04-27 10:14:36.564912 I | destination elasticsearch(http://127.0.0.1:9200)
The current dump position is : binlog.000015 2457

执行增量同步

复制代码
./go-mysql-transfer -config app.yml

数据类型映射表

|-----------|---------|---|
| mysql数据类型 | es数据类型 | |
| int | long | |
| varchar | keyword | |
| decimal | double | |

报错

同步数据报错

在 go-mysql-transfer 中执行

go get github.com/json-iterator/[email protected]

报错: document missing

复制代码
2024-05-01 17:15:16.081082 I | dumper stopped
2024-05-01 17:15:16.083295 I | transfer run from position(binlog.000017 6375)
2024-05-01 17:15:16.289692 I | [_doc][6]: document missing
2024-05-01 17:15:17.086846 I | transfer stop
2024-05-01 17:15:17.088804 I | dumper stopped
2024-05-01 17:15:17.091393 I | transfer run from position(binlog.000017 6375)
2024-05-01 17:15:17.294877 I | [_doc][6]: document missing
2024-05-01 17:15:18.095177 I | transfer stop
2024-05-01 17:15:18.097077 I | dumper stopped
2024-05-01 17:15:18.099320 I | transfer run from position(binlog.000017 6375)
2024-05-01 17:15:18.302411 I | [_doc][6]: document missing
^C2024-05-01 17:15:18.704188 I | application stoped锛𸱳ignal: interrupt 
2024-05-01 17:15:19.102070 I | transfer stop
2024-05-01 17:15:19.103859 I | dumper stopped

_doc 默认的索引类型 一个索引index 可以包含多个type

类型 type 包含多个文档(document)

原因: es 中没有数据,更新导致找不到对应的数据 因此需要先初始化数据到es 中

./go-mysql-transfer -stock

相关推荐
我来整一篇43 分钟前
用Redis的List实现消息队列
数据库·redis·list
加什么瓦1 小时前
Redis——数据结构
数据库·redis·缓存
神仙别闹1 小时前
基于C#+SQL Server开发(WinForm)租房管理系统
数据库·oracle·c#
528302 小时前
MySQL主从复制
数据库·mysql
qq_12498707532 小时前
原生小程序+springboot+vue医院医患纠纷管理系统的设计与开发(程序+论文+讲解+安装+售后)
java·数据库·spring boot·后端·小程序·毕业设计
jie188945758662 小时前
ubuntu----100,常用命令2
数据库·ubuntu
若兰幽竹2 小时前
【HBase整合Hive】HBase-1.4.8整合Hive-2.3.3过程
数据库·hive·hbase
lybugproducer2 小时前
浅谈 Redis 数据类型
java·数据库·redis·后端·链表·缓存
青山是哪个青山2 小时前
Redis 常见数据类型
数据库·redis·bootstrap
廖圣平2 小时前
美团核销 第三方接口供应商 (含接口文档)
开发语言·数据库·php