Giter Site home page Giter Site logo

zwunix / canal_mysql_elasticsearch_sync Goto Github PK

View Code? Open in Web Editor NEW

This project forked from starcwang/canal_mysql_elasticsearch_sync

0.0 1.0 0.0 72 KB

基于canal的mysql和elasticsearch实时同步方案,支持增量同步和全量同步

License: Apache License 2.0

Shell 3.99% Java 96.01%

canal_mysql_elasticsearch_sync's Introduction

canal_mysql_elasticsearch_sync 支持请star✨

canal于v1.1.2版本后,已支持自动同步到Elasticsearch。赞canal!canal传送门

基于 canalMysqlElasticsearch 实时同步的 javaweb 服务。
canal是阿里巴巴mysql数据库binlog的增量订阅&消费组件。

工作原理

全量

暴露Http接口(接口定义见wiki),待调用后开启后台线程,通过主键分批同步指定数据库中数据到Elasticsearch

读取数据库会加读锁
主键必须为数字类型

过程

  1. 首先会根据所给的数据库主键字段,拿到最大的主键数字max_id;
  2. pk=min_id(默认是数据库中的主键最小值);
  3. 加读锁🔐,从数据库中取出pk —— pk+stepSize 大小的数据(默认500)的数据;
  4. 插入到Elasticsearch中;
  5. 释放读锁🔐,pk累加stepSize,循环3.操作,直到pk>max_id

增量

循环监听canal通过binlog同步过来的event事件,区别增删改进行与之对应的Elasticsearch的操作。

目前只解析了 insert、update、delete,其它数据库操作会被忽略

默认相关字段映射

Mysql字段类型 Elasticsearch类型
char {"type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}}
text {"type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}}
blob {"type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}}
int {"type": "long"}
date {"type": "date"}
time {"type": "date"}
float {"type": "float"}
double {"type": "float"}
decimal {"type": "float"}
其它 {"type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}}

注意事项

  • Mysql的binlog格式必须为ROW
  • 因为有行锁,Mysql中table使用的存储引擎须为InnoDB
  • 由于使用binlog进行增量同步,和数据库主从类似,不可避免的会有一定的主从延迟,延迟时间取决于机房网络、机器负载、数据量大小等
  • Elasticsearch支持的版本为5.x
  • canal已测试版为v1.0.24,其他版本请自行测试
  • 增量同步只监听了 INSERT、UPDATE、DELETE,其它如建表、删表等尚未支持
  • 建议Elasticsearch的mapping手动来创建,因为默认的创建方式不能保证满足业务需求

相关文档

联系方式

如果有不合理的地方,还请不吝赐教。

支持记得star✨

canal_mysql_elasticsearch_sync's People

Contributors

starcwang avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.