Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ju4t/mysql-canal-es

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MySQL + Canal + Kafka + Elasticsearch + Kibana

MySQL --> Canal-Server --> MQ --> Canal-adapter --> ES

基础服务

$ docker compose up -d

MySQL 添加同步账号和测试表

$ mysql -uroot -p

-- 创建同步账号
CREATE USER canal IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
-- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES;

-- 创建测试数据
CREATE DATABASE testdb;

USE testdb;

CREATE TABLE `article` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `title` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `body` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `last_time` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`) USING BTREE
);

-- INSERT INTO `article`(`title`, `body`) VALUES ('标题', '正文内容');

Canal-server

配置已经包含在 docker-compose

验证:

# 日志路径 canal-server/logs/{$destination}/{$destination}.log
$ docker exec -it mysql-canal-es-canal-server-1 tail -f canal-server/logs/test/test.log

2023-01-05 16:52:48.915 [destination = test , address = mysql/172.25.0.3:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - ---> begin to find start position, it will be long time for reset or first position
2023-01-05 16:52:48.923 [destination = test , address = mysql/172.25.0.3:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - prepare to find start position just show master status
2023-01-05 16:52:51.450 [destination = test , address = mysql/172.25.0.3:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - ---> find start position successfully, EntryPosition[included=false,journalName=mysql-bin.000003,position=4,serverId=1,gtid=<null>,timestamp=1672908641000] cost : 2508ms , the next step is binlog dump

日志中可见 find start position successfully 即可

kafka 和 zk

配置已经包含在 docker-compose 中

  • 新建 topic

验证:

$ docker exec -it mysql-canal-es-kafka-1 sh -c '/opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --list'
my-topic

可见 my-topic 即可

在 ES 中创建 索引

在 Kibana 的 Dev Tools 中创建

http://localhost:5601/app/dev_tools#/console
# 建立索引
PUT /blog_article
{
  "settings": {
    "number_of_shards": "1",
    "number_of_replicas": "0"
  },
  "mappings": { 
    "properties": {
      "title":{
        "type": "text"
      },
      "body":{
        "type": "text"
      },
      "last_time":{
        "type": "date"
      }
    }
  }
}

验证:

# 验证索引
GET /_cat/indices
green open blog_article                    cOgb1VOmT2aFNmclLaQAhQ 1 0  0   0    226b    226b

# 多字段查询,默认情况下 汉字会被拆分每个字匹配,上结巴 分词插件
GET /blog_article/_search
{
  "query": {
    "multi_match": {
      "query": "关键词",
      "fields": ["title", "body"]
    }
  }
}

可见索引 blog_article 状态且为 green 即可

更多 es 的操作

# URL 查询
http://localhost:9200/blog_article/_search?q=title:哈哈

# 查询
GET /blog_article/_search
{
  "query": {
    "match": {
      "title": "哈哈"
    }
  },
  "_source": ["title", "body"],
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

# 多字段查询
GET /blog_article/_search
{
  "query": {
    "multi_match": {
      "query": "哈哈",
      "fields": ["title", "body"]
    }
  }
}

# 查看具体文档
GET /blog_article/_doc/1/

POST /blog_article/_open

GET /blog_article/_mapping

# 查看所有索引
GET /_cat/indices

# 删除索引
DELETE blog_article

客户端

使用 canal-adapter 跳过这步

$ export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
$ python canal-client.py

官方:canal-adapter 替代 canal-client.py https://github.com/alibaba/canal/releases/download/canal-1.1.6/canal.adapter-1.1.6.tar.gz

配置和启动 Canal-Adapter

修改配置文件

  • conf/application.yml
  • conf/es7/blog_article.yml

启动 canal-adapter

$ cd canal.adapter-1.1.6
$ bin/startup.sh
$ tail -f logs/adapter/adapter.log

测试

在 MySQL 中插入数据测试

插入、更新数据测试

INSERT INTO `article`(`title`, `body`) VALUES ('我是标题', '我是正文内容');
UPDATE `article` SET `title`='标题已改变' WHERE `id`=1;

在 Kibana 的 dev tools 中查看对应数据

# 多字段查询
GET /blog_article/_search
{
  "query": {
    "multi_match": {
      "query": "哈哈",
      "fields": ["title", "body"]
    }
  }
}

如果未在es中查找到结果,也许您需要手动ETL

# 手动 ETL
$ curl http://127.0.0.1:8081/etl/es7/blog_article.yml -X POST
{"succeeded":true,"resultMessage":"导入ES 数据:1 条"}%

$ curl http://127.0.0.1:8081/destinations
$ curl http://127.0.0.1:8081/syncSwitch/example/off -X PUT
$ curl http://127.0.0.1:8081/syncSwitch/test/on -X PUT
GET article/_search

About

MySQL + Canal + Kafka + ElasticSearch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published