在binlog中的数据类型
log_name | pos | event | serverid | endlogpos | info |
---|---|---|---|---|---|
mysql-bin.000025 | 364 | Query | 1 | 437 | begin |
mysql-bin.000025 | 437 | Query | 1 | 602 | DELETE FROM **(对应sql) |
mysql-bin.000025 | 602 | Xid | 1 | 629 | COMMIT /* xid=48 */ |
总得来说,虽然结构明显,但是多条数据凑合起来才能算一个操作,不是很好统计; 虽然python-mysql-replication的出现可以稍微处理一下,但是毕竟还是偏底层了一点;
因此我又发现了一个堪称神奇的东西:maxwell’s deamon;
虽然查询资料的时候,老是查到伟大物理学家麦克斯韦的订立,但是这丝毫不影响我继续将这个工具用下去的决心;
应用
- 标准输出如下:
标准输出
./maxwell --user='root' --password='ZTj8tInzrmXjpt0v' --host='106.75.147.184' --producer=stdout
它支持很多种消息工具:比如Kafka ,Kinesis , Google Cloud Pub/Sub ,RabbitMQ ,Redis;官网上说,它对kafka的支持是最好的,但是这里我觉得适合自己才是最好的;
一次采用redis的pub,sub;
- 输出到redis的pub:
输出到redis
./maxwell --user='root' --password='ZTj8tInzrmXjpt0v' --host='host' --producer=redis --redis_host='redis.hostname' ----redis_pub_channel="first"
--redis_host Host of Redis server
--redis_port Port of Redis server
--redis_auth Authentication key for a password-protected Redis server
--redis_database Database of Redis server
--redis_pub_channel Redis Pub/Sub channel for publishing records
--redis_list_key Redis LPUSH List Key for adding to a queue
--redis_type Selects either Redis Pub/Sub or LPUSH. Default to Pub/Sub
处理后的字段含义
- “type”:”insert”, 最常见的是你会在这里看到插入/更新/删除。如果你引导一个表格,你会看到“bootstrap-insert”,而DDL语句(稍后解释)有它们自己的类型。
- ”xid”:23396, 这是InnoDB与该行关联的交易的“交易ID”。在服务器的生命周期内它是唯一的,就我所知。
“server_id”:23042, 接受此事务的服务器的mysql server_id。
“thread_id”:108, thread_id或多或少是生成数据的客户端连接的唯一标识符。
“commit”:true,
如果您需要在流处理器中重新组装事务,则可以使用此字段并xid执行此操作。数据将如下所示:
没有commit,xid = 142
没有commit,xid = 142
行commit=true,xid = 142
没有commit,xid = 155
插入操作的处理效果
mysql> insert into test.e set m = 4.2341, c = now(3), comment = 'I am a creature of light.';
{
"database":"test",
"table":"e",
"type":"insert",
"ts":1477053217,
"xid":23396,
"commit":true,
"position":"master.000006:800911",
"server_id":23042,
"thread_id":108,
"data":{
"id":1,
"m":4.2341,
"c":"2016-10-21 05:33:37.523000",
"comment":"I am a creature of light."
}
}
更新操作的处理效果:
mysql> update test.e set m = 5.444, c = now(3) where id = 1;
{
"database":"test",
"table":"e",
"type":"update",
"ts":1477053234,
...
"data":{
"id":1,
"m":5.444,
"c":"2016-10-21 05:33:54.631000",
"comment":"I am a creature of light."
},
"old":{
"m":4.2341,
"c":"2016-10-21 05:33:37.523000"
}
}