binlog解析Demo:通过mysql-binlog-connector-java解析binlog二进制文件

1.引入依赖

<dependency>
    <groupId>com.github.shyiko</groupId>
    <artifactId>mysql-binlog-connector-java</artifactId>
    <version>0.18.1</version>
</dependency>

2.以文件流的形式读取binlog文件然后输出:

public static void main(String[] args) throws IOException {
        String filePath="D:\\mysql\\mysql-8.0.13-winx64\\log\\mysql-bin.000001";
        File binlogFile = new File(filePath);
        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(binlogFile, eventDeserializer);
        try {
            for (Event event; (event = reader.readEvent()) != null; ) {
                System.out.println(event.toString());
            }
        } finally {
            reader.close();
        }
     }

测试结果:

"C:\Program Files\Java\jdk1.8.0_202\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar=58499:D:\IntelliJ IDEA 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_202\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar;E:\驰星教育培训\项目\spring-esdome\target\classes;D:\Maven\repository\org\springframework\boot\spring-boot-starter-web\2.1.8.RELEASE\spring-boot-starter-web-2.1.8.RELEASE.jar;D:\Maven\repository\org\springframework\boot\spring-boot-starter\2.1.8.RELEASE\spring-boot-starter-2.1.8.RELEASE.jar;D:\Maven\repository\org\springframework\boot\spring-boot\2.1.8.RELEASE\spring-boot-2.1.8.RELEASE.jar;D:\Maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.8.RELEASE\spring-boot-autoconfigure-2.1.8.RELEASE.jar;D:\Maven\repository\org\springframework\boot\spring-boot-starter-logging\2.1.8.RELEASE\spring-boot-starter-logging-2.1.8.RELEASE.jar;D:\Maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;D:\Maven\repository\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;D:\Maven\repository\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;D:\Maven\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Maven\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\Maven\repository\org\springframework\boot\spring-boot-starter-json\2.1.8.RELEASE\spring-boot-starter-json-2.1.8.RELEASE.jar;D:\Maven\repository\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;D:\Maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;D:\Maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;D:\Maven\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;D:\Maven\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.8.RELEASE\spring-boot-starter-tomcat-2.1.8.RELEASE.jar;D:\Maven\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.24\tomcat-embed-core-9.0.24.jar;D:\Maven\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.24\tomcat-embed-el-9.0.24.jar;D:\Maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.24\tomcat-embed-websocket-9.0.24.jar;D:\Maven\repository\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;D:\Maven\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Maven\repository\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;D:\Maven\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\Maven\repository\org\springframework\spring-web\5.1.9.RELEASE\spring-web-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\spring-webmvc\5.1.9.RELEASE\spring-webmvc-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\boot\spring-boot-starter-data-elasticsearch\2.1.8.RELEASE\spring-boot-starter-data-elasticsearch-2.1.8.RELEASE.jar;D:\Maven\repository\org\springframework\data\spring-data-elasticsearch\3.1.10.RELEASE\spring-data-elasticsearch-3.1.10.RELEASE.jar;D:\Maven\repository\org\springframework\spring-tx\5.1.9.RELEASE\spring-tx-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\data\spring-data-commons\2.1.10.RELEASE\spring-data-commons-2.1.10.RELEASE.jar;D:\Maven\repository\joda-time\joda-time\2.10.3\joda-time-2.10.3.jar;D:\Maven\repository\org\elasticsearch\client\transport\6.4.3\transport-6.4.3.jar;D:\Maven\repository\org\elasticsearch\elasticsearch\6.4.3\elasticsearch-6.4.3.jar;D:\Maven\repository\org\elasticsearch\elasticsearch-core\6.4.3\elasticsearch-core-6.4.3.jar;D:\Maven\repository\org\elasticsearch\elasticsearch-secure-sm\6.4.3\elasticsearch-secure-sm-6.4.3.jar;D:\Maven\repository\org\elasticsearch\elasticsearch-x-content\6.4.3\elasticsearch-x-content-6.4.3.jar;D:\Maven\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-smile\2.9.9\jackson-dataformat-smile-2.9.9.jar;D:\Maven\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.9.9\jackson-dataformat-yaml-2.9.9.jar;D:\Maven\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-cbor\2.9.9\jackson-dataformat-cbor-2.9.9.jar;D:\Maven\repository\org\apache\lucene\lucene-core\7.4.0\lucene-core-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-analyzers-common\7.4.0\lucene-analyzers-common-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-backward-codecs\7.4.0\lucene-backward-codecs-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-grouping\7.4.0\lucene-grouping-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-highlighter\7.4.0\lucene-highlighter-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-join\7.4.0\lucene-join-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-memory\7.4.0\lucene-memory-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-misc\7.4.0\lucene-misc-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-queries\7.4.0\lucene-queries-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-queryparser\7.4.0\lucene-queryparser-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-sandbox\7.4.0\lucene-sandbox-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-spatial\7.4.0\lucene-spatial-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-spatial-extras\7.4.0\lucene-spatial-extras-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-spatial3d\7.4.0\lucene-spatial3d-7.4.0.jar;D:\Maven\repository\org\apache\lucene\lucene-suggest\7.4.0\lucene-suggest-7.4.0.jar;D:\Maven\repository\org\elasticsearch\elasticsearch-cli\6.4.3\elasticsearch-cli-6.4.3.jar;D:\Maven\repository\net\sf\jopt-simple\jopt-simple\5.0.2\jopt-simple-5.0.2.jar;D:\Maven\repository\com\carrotsearch\hppc\0.7.1\hppc-0.7.1.jar;D:\Maven\repository\com\tdunning\t-digest\3.2\t-digest-3.2.jar;D:\Maven\repository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;D:\Maven\repository\org\elasticsearch\jna\4.5.1\jna-4.5.1.jar;D:\Maven\repository\org\elasticsearch\plugin\reindex-client\6.4.3\reindex-client-6.4.3.jar;D:\Maven\repository\org\elasticsearch\client\elasticsearch-rest-client\6.4.3\elasticsearch-rest-client-6.4.3.jar;D:\Maven\repository\org\apache\httpcomponents\httpclient\4.5.9\httpclient-4.5.9.jar;D:\Maven\repository\org\apache\httpcomponents\httpcore\4.4.12\httpcore-4.4.12.jar;D:\Maven\repository\org\apache\httpcomponents\httpasyncclient\4.1.4\httpasyncclient-4.1.4.jar;D:\Maven\repository\org\apache\httpcomponents\httpcore-nio\4.4.12\httpcore-nio-4.4.12.jar;D:\Maven\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\Maven\repository\org\elasticsearch\plugin\lang-mustache-client\6.4.3\lang-mustache-client-6.4.3.jar;D:\Maven\repository\com\github\spullara\mustache\java\compiler\0.9.3\compiler-0.9.3.jar;D:\Maven\repository\org\elasticsearch\plugin\percolator-client\6.4.3\percolator-client-6.4.3.jar;D:\Maven\repository\org\elasticsearch\plugin\parent-join-client\6.4.3\parent-join-client-6.4.3.jar;D:\Maven\repository\org\elasticsearch\plugin\rank-eval-client\6.4.3\rank-eval-client-6.4.3.jar;D:\Maven\repository\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;D:\Maven\repository\org\elasticsearch\plugin\transport-netty4-client\6.4.3\transport-netty4-client-6.4.3.jar;D:\Maven\repository\io\netty\netty-buffer\4.1.39.Final\netty-buffer-4.1.39.Final.jar;D:\Maven\repository\io\netty\netty-codec\4.1.39.Final\netty-codec-4.1.39.Final.jar;D:\Maven\repository\io\netty\netty-codec-http\4.1.39.Final\netty-codec-http-4.1.39.Final.jar;D:\Maven\repository\io\netty\netty-common\4.1.39.Final\netty-common-4.1.39.Final.jar;D:\Maven\repository\io\netty\netty-handler\4.1.39.Final\netty-handler-4.1.39.Final.jar;D:\Maven\repository\io\netty\netty-resolver\4.1.39.Final\netty-resolver-4.1.39.Final.jar;D:\Maven\repository\io\netty\netty-transport\4.1.39.Final\netty-transport-4.1.39.Final.jar;D:\Maven\repository\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;D:\Maven\repository\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;D:\Maven\repository\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;D:\Maven\repository\com\github\shyiko\mysql-binlog-connector-java\0.18.1\mysql-binlog-connector-java-0.18.1.jar" com.czxy.binlogutil.Sample
Event{header=EventHeaderV4{timestamp=1570352811000, eventType=FORMAT_DESCRIPTION, serverId=1, headerLength=19, dataLength=84, nextPosition=107, flags=1}, data=FormatDescriptionEventData{binlogVersion=4, serverVersion='5.5.28-log', headerLength=19, dataLength=84, checksumType=NONE}}
Event{header=EventHeaderV4{timestamp=1570352823000, eventType=QUERY, serverId=1, headerLength=19, dataLength=53, nextPosition=179, flags=8}, data=QueryEventData{threadId=2, executionTime=0, errorCode=0, database='seasonal', sql='BEGIN'}}
Event{header=EventHeaderV4{timestamp=1570352823000, eventType=QUERY, serverId=1, headerLength=19, dataLength=121, nextPosition=319, flags=0}, data=QueryEventData{threadId=2, executionTime=0, errorCode=0, database='seasonal', sql='UPDATE `compose_good` SET `compose_good_name`='abc' WHERE (`id`='100136')'}}
Event{header=EventHeaderV4{timestamp=1570352823000, eventType=XID, serverId=1, headerLength=19, dataLength=8, nextPosition=346, flags=0}, data=XidEventData{xid=7}}
Event{header=EventHeaderV4{timestamp=1570352867000, eventType=QUERY, serverId=1, headerLength=19, dataLength=53, nextPosition=418, flags=8}, data=QueryEventData{threadId=3, executionTime=0, errorCode=0, database='seasonal', sql='BEGIN'}}
Event{header=EventHeaderV4{timestamp=1570352867000, eventType=QUERY, serverId=1, headerLength=19, dataLength=109, nextPosition=546, flags=0}, data=QueryEventData{threadId=3, executionTime=0, errorCode=0, database='seasonal', sql='UPDATE `good` SET `create_time`='2019-09-19' WHERE (`id`='5')'}}
Event{header=EventHeaderV4{timestamp=1570352867000, eventType=XID, serverId=1, headerLength=19, dataLength=8, nextPosition=573, flags=0}, data=XidEventData{xid=18}}
Event{header=EventHeaderV4{timestamp=1570353445000, eventType=QUERY, serverId=1, headerLength=19, dataLength=53, nextPosition=645, flags=8}, data=QueryEventData{threadId=5, executionTime=0, errorCode=0, database='seasonal', sql='BEGIN'}}
Event{header=EventHeaderV4{timestamp=1570353445000, eventType=INTVAR, serverId=1, headerLength=19, dataLength=9, nextPosition=673, flags=0}, data=IntVarEventData{type=2, value=100137}}
Event{header=EventHeaderV4{timestamp=1570353445000, eventType=QUERY, serverId=1, headerLength=19, dataLength=390, nextPosition=1082, flags=0}, data=QueryEventData{threadId=5, executionTime=0, errorCode=0, database='seasonal', sql='INSERT INTO `compose_good` (`compose_good_name`, `compose_good_price`, `compose_good_type`, `compose_good_status`, `compose_good_weight`, `compose_good_icon`, `compose_good_sales`, `comment_number`, `compose_good_describe`, `create_time`, `update_time`) VALUES ('陆旭', '0', '0', '0', '3000', '0', '0', '0', '0', '2019-11-01', '2019-10-06')'}}
Event{header=EventHeaderV4{timestamp=1570353445000, eventType=XID, serverId=1, headerLength=19, dataLength=8, nextPosition=1109, flags=0}, data=XidEventData{xid=27}}

这样 就可以根据需求去提取其中的数据了然后进行你的逻辑即可。

你可能感兴趣的:(mysql)