canal客户端 php,Canal 使用入门 - 客户端

本文演示了如何配置Canal客户端,连接到Canal服务器并监听数据库变更。通过修改POM文件添加Canal依赖,创建测试类并实现数据监听,当对数据库中的TableA进行INSERT、UPDATE和DELETE操作时,Canal能够捕获并打印这些变更事件。
摘要由CSDN通过智能技术生成

1.修改POM文件(pom.xml),添加依赖:

com.alibaba.otter

canal.client

1.0.12

2.建立测试类:

TestCanal.java

import java.net.InetSocketAddress;

import java.util.List;

import com.alibaba.otter.canal.client.CanalConnector;

import com.alibaba.otter.canal.common.utils.AddressUtils;

import com.alibaba.otter.canal.protocol.Message;

import com.alibaba.otter.canal.protocol.CanalEntry.Column;

import com.alibaba.otter.canal.protocol.CanalEntry.Entry;

import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;

import com.alibaba.otter.canal.protocol.CanalEntry.EventType;

import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;

import com.alibaba.otter.canal.protocol.CanalEntry.RowData;

import com.alibaba.otter.canal.client.*;

public class TestCanal {

public static void main(String args[]) {

// 创建链接

CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(AddressUtils.getHostIp(),

11111), "example", "", "");

int batchSize = 1000;

int emptyCount = 0;

try {

connector.connect();

connector.subscribe(".*\..*");

connector.rollback();

int totalEmtryCount = 1200;

while (emptyCount < totalEmtryCount) {

Message message = connector.getWithoutAck(batchSize); // 获取指定数量的数据

long batchId = message.getId();

int size = message.getEntries().size();

if (batchId == -1 || size == 0) {

emptyCount++;

System.out.println("empty count : " + emptyCount);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

} else {

emptyCount = 0;

// System.out.printf("message[batchId=%s,size=%s]

", batchId, size);

printEntry(message.getEntries());

}

connector.ack(batchId); // 提交确认

// connector.rollback(batchId); // 处理失败, 回滚数据

}

System.out.println("empty too many times, exit");

} finally {

connector.disconnect();

}

}

private static void printEntry(List entrys) {

for (Entry entry : entrys) {

if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {

continue;

}

RowChange rowChage = null;

try {

rowChage = RowChange.parseFrom(entry.getStoreValue());

} catch (Exception e) {

throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),

e);

}

EventType eventType = rowChage.getEventType();

System.out.println(String.format("================> binlog[%s:%s] , name[%s,%s] , eventType : %s",

entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),

entry.getHeader().getSchemaName(), entry.getHeader().getTableName(),

eventType));

for (RowData rowData : rowChage.getRowDatasList()) {

if (eventType == EventType.DELETE) {

printColumn(rowData.getBeforeColumnsList());

} else if (eventType == EventType.INSERT) {

printColumn(rowData.getAfterColumnsList());

} else {

System.out.println("-------> before");

printColumn(rowData.getBeforeColumnsList());

System.out.println("-------> after");

printColumn(rowData.getAfterColumnsList());

}

}

}

}

private static void printColumn(List columns) {

for (Column column : columns) {

System.out.println(column.getName() + " : " + column.getValue() + "    update=" + column.getUpdated());

}

}

}

3.运行测试类,可以看到控制台如下输出:

empty count : 1

empty count : 2

empty count : 3

4.此时通过SQL对数据进行操作:

INSERT TABLEA (ID, NAME) VALUES (1, "A");

INSERT TABLEA (ID, NAME) VALUES (2, "B");

INSERT TABLEA (ID, NAME) VALUES (3, "C");

UPDATE TABLEA SET NAME = "AA" WHERE ID = 1;

DELETE FROM TABLEA WHERE ID = 1;

5.在控制台中可以查看到数据库操作的推送信息:

================> binlog[mysql-bin.000001:13290] , name[test,tablea] , eventType : INSERT

id : 1    update=true

name : A    update=true

id : 2    update=true

name : B    update=true

id : 3    update=true

name : C    update=true

================> binlog[mysql-bin.000001:13466] , name[test,tablea] , eventType : UPDATE

-------> before

id : 1    update=false

name : A    update=false

-------> after

id : 1    update=false

name : AA    update=true

================> binlog[mysql-bin.000001:13874] , name[test,tablea] , eventType : DELETE

id : 1    update=false

name : AA    update=false

id : 2    update=false

name : B    update=false

id : 3    update=false

name : C    update=false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值