package test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import com.github.shyiko.mysql.binlog.BinaryLogFileReader;
import com.github.shyiko.mysql.binlog.event.Event;
import com.github.shyiko.mysql.binlog.event.EventData;
import com.github.shyiko.mysql.binlog.event.QueryEventData;
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData;
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;
import cn.hutool.json.JSONObject;
public class Binlog {
public static void main(String[] args) throws IOException {
String filePath="I:\\mysql-bin.000038";
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 {
String recordIds = "('-1'";
for (Event event; (event = reader.readEvent()) != null; ) {
EventData data = event.getData();
if(data!=null)
{
// System.out.println(data.getClass());
if (data instanceof QueryEventData) {
QueryEventData wData =(QueryEventData)data;
//wData.getIncludedColumns();
// wData.getRows();
String sql = wData.getSql();
BufferedWriter writer = new BufferedWriter(new FileWriter("I:\\sql.txt",true));
writer.append(wData.getSql().replace("\r", "").replace("\n", "")+"\r\n");
writer.close();
}
}
}
} finally {
reader.close();
}
}
}
Maven 配置
<dependency>
<groupId>com.github.shyiko</groupId>
<artifactId>mysql-binlog-connector-java</artifactId>
<version>0.13.0</version>
</dependency>
原文:https://ask.hellobi.com/blog/cimen/9133