工具包:java客户端读取mysql binglog

java客户端读取mysql binglog

git地址:https://github.com/shyiko/mysql-binlog-connector-java

pom引入:

com.github.shyiko
mysql-binlog-connector-java
0.18.1

测试代码:
package com.sxzhongf.ad.service;

import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.DeleteRowsEventData;
import com.github.shyiko.mysql.binlog.event.EventData;
import com.github.shyiko.mysql.binlog.event.UpdateRowsEventData;
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData;

import java.io.IOException;

/**

  • BinlogServiceTest for 测试Mysql binlog 监控

  • {@code

  • Mysql8 连接提示 Client does not support authentication protocol requested by server; consider upgrading MySQL client 解决方法

  • USE mysql;

  • ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;

  • FLUSH PRIVILEGES;

  • }
    */
    public class BinlogServiceTest {

    /**

    • --------Update-----------
    • UpdateRowsEventData{tableId=90, includedColumnsBeforeUpdate={0, 1, 2, 3, 4, 5, 6, 7}, includedColumns={0, 1, 2, 3, 4, 5, 6, 7}, rows=[
    • {before=[11, 10, Test Bin Log, 1, Tue Jun 25 08:00:00 CST 2019, Tue Jun 25 08:00:00 CST 2019, Tue Jun 25 08:00:00 CST 2019, Tue Jun 25 08:00:00 CST 2019], after=[11, 10, zhangpan test Binlog, 1, Tue Jun 25 08:00:00 CST 2019, Tue Jun 25 08:00:00 CST 2019, Tue Jun 25 08:00:00 CST 2019, Tue Jun 25 08:00:00 CST 2019]}
      
    • ]}
    • --------Insert-----------
    • WriteRowsEventData{tableId=91, includedColumns={0, 1, 2, 3, 4, 5, 6, 7}, rows=[
    • [10, 11, ad unit test binlog, 1, 0, 1236.7655, Thu Jun 27 08:00:00 CST 2019, Thu Jun 27 08:00:00 CST 2019]
      
    • ]}
      */

    public static void main(String[] args) throws IOException {

// //构造BinaryLogClient,填充mysql链接信息
BinaryLogClient client = new BinaryLogClient(“127.0.0.1”, 3306,
“root”, “12345678”
);

      //设置需要读取的Binlog的文件以及位置,否则,client会从"头"开始读取Binlog并监听

// client.setBinlogFilename(“binlog.000035”);
// client.setBinlogPosition();

      //给客户端注册监听器,实现对Binlog的监听和解析
      //event 就是监听到的Binlog变化信息,event包含header & data 两部分
      client.registerEventListener(event -> {
          EventData data = event.getData();
          if (data instanceof UpdateRowsEventData) {
              System.out.println("--------Update-----------");
              System.out.println(data.toString());
          } else if (data instanceof WriteRowsEventData) {
              System.out.println("--------Insert-----------");
              System.out.println(data.toString());
          } else if (data instanceof DeleteRowsEventData) {
              System.out.println("--------Delete-----------");
              System.out.println(data.toString());
          }
      });

      client.connect();
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值