Java读取MySql的binlog日志
·
- 在pom.xml中添加jar包
<dependency>
<groupId>com.github.shyiko</groupId>
<artifactId>mysql-binlog-connector-java</artifactId>
<version>0.20.1</version>
</dependency>
- 读取指定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();
}
}
}
拿到结果:
更多推荐
已为社区贡献1条内容
所有评论(0)