1. 在pom.xml中添加jar包
        <dependency>
            <groupId>com.github.shyiko</groupId>
            <artifactId>mysql-binlog-connector-java</artifactId>
            <version>0.20.1</version>
        </dependency>
  1. 读取指定binlog文件里的内容
public class Application {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\binlog.000005");
        EventDeserializer eventDeserializer = new EventDeserializer();
        eventDeserializer.setCompatibilityMode(
                EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
                EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY
        );
        BinaryLogFileReader reader = new BinaryLogFileReader(file, eventDeserializer);
        try {
            for (Event event; (event = reader.readEvent()) != null; ) {
                EventData data = event.getData();
                if (data != null && data.getClass().isAssignableFrom(RowsQueryEventData.class)) {
                    RowsQueryEventData dmlData = (RowsQueryEventData) data;
                    System.out.println(dmlData.getQuery());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
    }
}

拿到结果:
在这里插入图片描述

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐