【canal】canal同步msyql到redis

java层面 canal client配置pom文件

复制代码
     <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.13.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>33.5.0-jre</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.otter</groupId>
            <artifactId>canal.client</artifactId>
            <version>1.1.7</version> <!-- 使用最新版本 -->
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <!-- Redisson -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.23.5</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.otter</groupId>
            <artifactId>canal.protocol</artifactId>
            <version>1.1.7</version>
        </dependency>

java client

1.将接收到的canal数据2进制转为10进制数据

2.redis client需要2进制转10进制,否则key是10进制,内部还是2进制

java 复制代码
package org.example.cannal.redis;

import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.protocol.Message;
import com.alibaba.otter.canal.protocol.CanalEntry.*;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CanalBinaryParseWithRedisson {

    public static void main(String[] args) {
        // 1. 创建 Canal 连接
        CanalConnector connector = CanalConnectors.newSingleConnector(
                new InetSocketAddress("127.0.0.1", 11111),
                "example", "", ""
        );

        // 2. 创建 Redisson 客户端
        RedissonClient redisson = createRedissonClient();

        try {
            connector.connect();
            connector.subscribe(".*\\..*");
            connector.rollback();

            while (true) {
                Message message = connector.getWithoutAck(100); // 每次取100条
                long batchId = message.getId();
                int size = message.getEntries().size();

                if (batchId == -1 || size == 0) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ignored) {}
                } else {
                    handleEntries(message.getEntries(), redisson);
                    connector.ack(batchId); // 确认消费
                }
            }
        } finally {
            connector.disconnect();
            redisson.shutdown();
        }
    }

    private static void handleEntries(List<Entry> entrys, RedissonClient redisson) {
        for (Entry entry : entrys) {
            if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN ||
                entry.getEntryType() == EntryType.TRANSACTIONEND) {
                continue;
            }

            RowChange rowChange;
            try {
                // 关键:把二进制解析成 RowChange
                rowChange = RowChange.parseFrom(entry.getStoreValue());
            } catch (Exception e) {
                throw new RuntimeException("解析 RowChange 失败", e);
            }

            EventType eventType = rowChange.getEventType();
            String tableName = entry.getHeader().getTableName();

            for (RowData rowData : rowChange.getRowDatasList()) {
                if (eventType == EventType.INSERT || eventType == EventType.UPDATE) {
                    Map<String, String> dataMap = new HashMap<>();
                    String id = null;
                    for (Column column : rowData.getAfterColumnsList()) {
                        dataMap.put(column.getName(), column.getValue());
                        if ("id".equals(column.getName())) {
                            id = column.getValue();
                        }
                    }
                    // 用 Redisson 写入 Redis Hash
                    RMap<String, String> map = redisson.getMap(tableName + ":" + id);
                    map.putAll(dataMap);
                    System.out.println("写入 Redis: " + tableName + ":" + id + " -> " + dataMap);
                } else if (eventType == EventType.DELETE) {
                    String id = null;
                    for (Column column : rowData.getBeforeColumnsList()) {
                        if ("id".equals(column.getName())) {
                            id = column.getValue();
                            break;
                        }
                    }
                    // 删除 Redis Key
                    redisson.getMap(tableName + ":" + id).delete();
                    System.out.println("删除 Redis: " + tableName + ":" + id);
                }
            }
        }
    }

    private static RedissonClient createRedissonClient() {
        Config config = new Config();
        // 1. 配置Redis连接(单节点为例)
        config.useSingleServer()
                .setAddress("redis://127.0.0.1:6379")
//                .setPassword("你的密码") // 无密码则省略
                .setDatabase(1);

        // 2. 关键:设置序列化器为StringCodec(明文字符串序列化)
        config.setCodec(new org.redisson.client.codec.StringCodec());

        return org.redisson.Redisson.create(config);
    }
}
相关推荐
lili-felicity1 分钟前
CANN批处理优化技巧:从动态批处理到流水线并行
人工智能·python
一个有梦有戏的人3 分钟前
Python3基础:进阶基础,筑牢编程底层能力
后端·python
独行soc6 分钟前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
摘星编程20 分钟前
解析CANN ops-nn中的Transpose算子:张量维度变换的高效实现
python
Liekkas Kono28 分钟前
RapidOCR Python 贡献指南
开发语言·python·rapidocr
王码码203536 分钟前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
2501_9151063237 分钟前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
玄同76540 分钟前
Python 后端三剑客:FastAPI/Flask/Django 对比与 LLM 开发选型指南
人工智能·python·机器学习·自然语言处理·django·flask·fastapi
爱吃泡芙的小白白1 小时前
环境数据多维关系探索利器:Pairs Plot 完全指南
python·信息可视化·数据分析·环境领域·pairs plot