Canal之ClientSample
来源:互联网 发布:淘宝内衣哪些有买家秀 编辑:程序博客网 时间:2024/06/01 09:25
ClientSample
直接使用canal.example工程
a. 首先启动Canal Server,可参见Canal快速开始
b.
1. 可以在eclipse里,直接打开com.alibaba.otter.canal.example.SimpleCanalClientTest,直接运行
2. 在工程的example目录下运行命令行:
mvn exec:java -Dexec.mainClass="com.alibaba.otter.canal.example.SimpleCanalClientTest"
- 下载example包: https://github.com/alibaba/canal/releases,解压缩后,直接运行sh startup.sh脚本
c. 触发数据变更
d. 在控制台或者logs中查看,可以看到如下信息 :
================> binlog[mysql-bin.002579:508882822] , name[retl,xdual] , eventType : UPDATE , executeTime : 1368607728000 , delay : 4270ms-------> beforeID : 1 update=falseX : 2013-05-15 11:43:42 update=false-------> afterID : 1 update=falseX : 2013-05-15 16:48:48 update=true
从头创建工程
依赖配置:
<dependency> <groupId>com.alibaba.otter</groupId> <artifactId>canal.client</artifactId> <version>1.0.12</version></dependency>
- 创建mvn标准工程:
mvn archetype:create -DgroupId=com.alibaba.otter -DartifactId=canal.sample
修改pom.xml,添加依赖
ClientSample代码
package com.alibaba.otter.canal.sample;import java.net.InetSocketAddress;import java.util.List;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;public class SimpleCanalClientExample { 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 = 120; 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) { } } else { emptyCount = 0; // System.out.printf("message[batchId=%s,size=%s] \n", 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<Entry> 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<Column> columns) { for (Column column : columns) { System.out.println(column.getName() + " : " + column.getValue() + " update=" + column.getUpdated()); } }}
- 运行Client
首先启动Canal Server,可参见Canal快速开始
启动Canal Client后,可以从控制台从看到类似消息:
empty count : 1empty count : 2empty count : 3empty count : 4
此时代表当前数据库无变更数据
- 触发数据库变更
mysql> use test;Database changedmysql> CREATE TABLE `xdual` ( -> `ID` int(11) NOT NULL AUTO_INCREMENT, -> `X` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -> PRIMARY KEY (`ID`) -> ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ;Query OK, 0 rows affected (0.06 sec)mysql> insert into xdual(id,x) values(null,now());Query OK, 1 row affected (0.06 sec)
可以从控制台中看到:
empty count : 1empty count : 2empty count : 3empty count : 4================>; binlog[mysql-bin.001946:313661577] , name[test,xdual] , eventType : INSERTID : 4 update=trueX : 2013-02-05 23:29:46 update=true
最后:
如果需要更详细的exmpale例子,请下载canal当前最新源码包,里面有个example工程,谢谢.
- Simple客户端例子:SimpleCanalClientTest
- Cluster客户端例子:ClusterCanalClientTest
本文为https://github.com/alibaba/canal项目文档,仅作个人记录使用
0 0
- Canal之ClientSample
- Canal之配置
- Canal之Client API
- Canal之配置
- canal
- canal概述
- canal DevGuide
- Canal笔记
- Canal简介
- canal安装
- canal简介
- Canal配置
- Canal详解
- canal 坑
- canal简介
- canal配置
- canal配置
- Canal AdminGuide canal信息介绍
- Android处理图片OOM的若干方法小结
- poj1113 Wall
- JPEG解码,解决JPG图片显示成花屏的问题
- java SOLID原则
- Android 集成百度统计
- Canal之ClientSample
- 枚举问题总结
- SFC 修复失败
- OBS二次开发随记
- Raptor实践参考:大或小
- Android中为什么主线程不会因为Looper.loop()里的死循环卡死?
- iOS - iOS ,Mac 开源项目以及库,知识点
- Activity四种启动模式
- Android Studio 下获取debug sha1和md5