canal+mq将数据同步到redis中的一些类型转换问题

在将 Canal 捕获到的数据库变更同步到 RabbitMQ 时,通常需要将变更事件的数据从 Java 对象转换为一种通用的数据格式,如 JSON。这样可以确保数据在不同系统之间传递时的兼容性。以下是将 Canal 数据同步到 RabbitMQ 并进行数据类型转换的示例代码。

1. Canal 客户端从 MySQL 读取变更

首先,编写 Canal 客户端以从 MySQL 读取数据库变更,并将变更数据转换为 JSON 格式,然后发送到 RabbitMQ。

2. 依赖库

在项目中添加了所需的依赖库,如 Canal 客户端、RabbitMQ 客户端和 JSON 处理库(例如 Jackson)。

3. Canal 客户端发送数据到 RabbitMQ

编写 Canal 客户端,读取 MySQL 数据库变更并将其发送到 RabbitMQ:

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.Message;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

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

public class CanalToRabbitMQ {
    private final static String QUEUE_NAME = "db_changes";
    private final static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {
        CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("127.0.0.1", 11111), "example", "", "");

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);

            connector.connect();
            connector.subscribe("mydatabase.mytable");
            connector.rollback();

            while (true) {
                Message message = connector.getWithoutAck(100); // 获取指定数量的数据
                long batchId = message.getId();
                int size = message.getEntries().size();
                if (batchId != -1 && size > 0) {
                    for (CanalEntry.Entry entry : message.getEntries()) {
                        if (entry.getEntryType() == CanalEntry.EntryType.ROWDATA) {
                            CanalEntry.RowChange rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
                            for (CanalEntry.RowData rowData : rowChange.getRowDatasList()) {
                                Map<String, Object> dataMap = new HashMap<>();
                                if (rowChange.getEventType() == CanalEntry.EventType.INSERT) {
                                    dataMap.put("eventType", "INSERT");
                                    for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
                                        dataMap.put(column.getName(), column.getValue());
                                    }
                                } else if (rowChange.getEventType() == CanalEntry.EventType.UPDATE) {
                                    dataMap.put("eventType", "UPDATE");
                                    for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
                                        dataMap.put(column.getName(), column.getValue());
                                    }
                                } else if (rowChange.getEventType() == CanalEntry.EventType.DELETE) {
                                    dataMap.put("eventType", "DELETE");
                                    for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
                                        dataMap.put(column.getName(), column.getValue());
                                    }
                                }
                                
                                // 将数据转换为 JSON 字符串
                                String jsonMessage = objectMapper.writeValueAsString(dataMap);
                                
                                // 将 JSON 字符串发送到 RabbitMQ
                                channel.basicPublish("", QUEUE_NAME, null, jsonMessage.getBytes("UTF-8"));
                            }
                        }
                    }
                }
                connector.ack(batchId); // 提交确认
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connector.disconnect();
        }
    }
}

4. RabbitMQ 消费者同步到 Redis

编写 RabbitMQ 消费者,从 RabbitMQ 队列中消费消息并同步到 Redis:

 
import com.rabbitmq.client.*;
import redis.clients.jedis.Jedis;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class RabbitMQToRedis {
    private final static String QUEUE_NAME = "db_changes";
    private final static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        Jedis jedis = new Jedis("127.0.0.1");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            try {
                // 将 JSON 字符串转换为 Map 对象
                Map<String, Object> dataMap = objectMapper.readValue(message, Map.class);

                // 处理数据并同步到 Redis
                String eventType = (String) dataMap.get("eventType");
                if ("INSERT".equals(eventType) || "UPDATE".equals(eventType)) {
                    // 假设主键是 id
                    String id = (String) dataMap.get("id");
                    jedis.set(id, message);
                } else if ("DELETE".equals(eventType)) {
                    String id = (String) dataMap.get("id");
                    jedis.del(id);
                }

                System.out.println("Received and processed: " + message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };

        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {});

        // 保持程序运行
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                channel.close();
                connection.close();
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
    }
}

代码解析

  1. Canal 客户端

    • 连接到 Canal Server,订阅指定数据库和表的变更。
    • 从 binlog 中解析变更事件。
    • 将变更事件转换为 JSON 格式。
    • 将 JSON 消息发送到 RabbitMQ。
  2. RabbitMQ 消费者

    • 连接到 RabbitMQ 队列,消费消息。
    • 将 JSON 消息解析为 Map 对象。
    • 根据事件类型(INSERT、UPDATE、DELETE)同步数据到 Redis。

通过这种方式,Canal 可以将数据库变更事件捕获并转换为 JSON 格式,推送到 RabbitMQ,然后由 RabbitMQ 消费者同步到 Redis。这种方法确保了数据在不同系统之间传递时的兼容性和灵活性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值