Alibaba Canal
什么是canal
官方的解释是:基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了mysql。按我的理解就是获取并解析mysql binlog的一个工具。详情可参考canal
原理
简单来说就是把自己伪装成mysql slave
canal client示例代码
import java.net.InetSocketAddress;
import java.util.List;
import com.alibaba.otter.canal.client.CanalConnectors;
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;
public class SimpleCanalClientExample {
public static void main(String args[]) {
// 创建链接
CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(AddressUtils.getHostIp(),
11111), "example", "", ""); // port是默认的端口,如果server的端口你自己修改过,这里要改成对应的
int batchSize = 512;
int emptyCount = 0;
try {
connector.connect();
connector.subscribe(".*\\..*"); //订阅当前链接下所有数据库下所有表的变更。如果你只想看某个表也可以改成你自己的形式,如test\\..*,test\\.mytable
connector.rollback();
int totalEmptyCount = 120; // 做个限制,防止无限循环,你也可以自己改大
while (emptyCount < totalEmptyCount) {
Message message = connector.getWithoutAck(batchSize); // 获取指定数量的数据,getWithoutAck在canal文档中有详细介绍
System.out.println(message.toString());
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); // 处理失败, 回滚数据。rollback需要你自己加个判断
}
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());
}
}
}
具体一条message
Message[id=3,entries=[header {
version: 1
logfileName: "mysql-bin.000002"
logfileOffset: 685
serverId: 1
serverenCode: "UTF-8"
executeTime: 1545980851000
sourceType: MYSQL
schemaName: ""
tableName: ""
eventLength: 72
props {
key: "curtGtid"
value: "ba229943-d845-11e8-9fcc-0492264bb587:3"
}
props {
key: "curtGtidSn"
value: "null"
}
props {
key: "curtGtidLct"
value: "null"
}
gtid: "ba229943-d845-11e8-9fcc-0492264bb587:1-3"
}
entryType: TRANSACTIONBEGIN
storeValue: " \004"
, header {
version: 1
logfileName: "mysql-bin.000002"
logfileOffset: 807
serverId: 1
serverenCode: "UTF-8"
executeTime: 1545980851000
sourceType: MYSQL
schemaName: "test"
tableName: "test"
eventLength: 42
eventType: INSERT
props {
key: "curtGtid"
value: "ba229943-d845-11e8-9fcc-0492264bb587:3"
}
props {
key: "curtGtidSn"
value: "null"
}
props {
key: "curtGtidLct"
value: "null"
}
props {
key: "rowsCount"
value: "1"
}
gtid: "ba229943-d845-11e8-9fcc-0492264bb587:1-3"
}
entryType: ROWDATA
storeValue: "\bF\020\001P\000b?\022\032\b\000\020\004\032\002id \001(\0010\000B\0015R\aint(11)\022!\b\001\020\f\032\004name \000(\0010\000B\0015R\fvarchar(255)"
, header {
version: 1
logfileName: "mysql-bin.000002"
logfileOffset: 849
serverId: 1
serverenCode: "UTF-8"
executeTime: 1545980851000
sourceType: MYSQL
schemaName: ""
tableName: ""
eventLength: 31
props {
key: "curtGtid"
value: "ba229943-d845-11e8-9fcc-0492264bb587:3"
}
props {
key: "curtGtidSn"
value: "null"
}
props {
key: "curtGtidLct"
value: "null"
}
gtid: "ba229943-d845-11e8-9fcc-0492264bb587:1-3"
}
entryType: TRANSACTIONEND
storeValue: "\022\00235"
],raw=true,rawEntries=[]]