forked from xin497668869/binlog2sql_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinlog2Sql.java
More file actions
99 lines (85 loc) · 3.72 KB
/
Binlog2Sql.java
File metadata and controls
99 lines (85 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.seewo;
import com.google.code.or.OpenReplicator;
import com.seewo.binlog2sql.MyBinlogParser;
import com.seewo.binlog2sql.eventhandle.DeleteHandle;
import com.seewo.binlog2sql.eventhandle.InsertHandle;
import com.seewo.binlog2sql.eventhandle.TableMapHandle;
import com.seewo.binlog2sql.eventhandle.UpdateHandle;
import com.seewo.vo.DbInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import static com.google.code.or.common.util.MySQLConstants.DELETE_ROWS_EVENT;
import static com.google.code.or.common.util.MySQLConstants.DELETE_ROWS_EVENT_V2;
import static com.google.code.or.common.util.MySQLConstants.TABLE_MAP_EVENT;
import static com.google.code.or.common.util.MySQLConstants.UPDATE_ROWS_EVENT;
import static com.google.code.or.common.util.MySQLConstants.UPDATE_ROWS_EVENT_V2;
import static com.google.code.or.common.util.MySQLConstants.WRITE_ROWS_EVENT;
import static com.google.code.or.common.util.MySQLConstants.WRITE_ROWS_EVENT_V2;
/**
* @author [email protected]
* @version 1.0
* @description
*/
public class Binlog2Sql {
public static final Logger LOGGER = LoggerFactory.getLogger(Binlog2Sql.class);
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
LOGGER.error("没找到jdbc类, 无法使用MYSQL类");
}
}
public static String getFirstBinLogName(OpenReplicator or) {
String url = "jdbc:mysql://" + or.getHost() + ":" + or.getPort() + "/mysql";
try (Connection conn = DriverManager.getConnection(url, or.getUser(), or.getPassword()); Statement statement = conn.createStatement()) {
ResultSet resultSet = statement.executeQuery("show master logs;");
while (resultSet.next()) {
String logName = resultSet.getString("Log_name");
return logName;
}
} catch (SQLException e) {
LOGGER.error("获取binlogName失败", e);
}
return null;
}
public static void main(String[] args) throws Exception {
DbInfoVo dbInfoVo = new DbInfoVo();
dbInfoVo.setHost("localhost");
dbInfoVo.setPort(3306);
dbInfoVo.setUsername("root");
dbInfoVo.setPassword("root");
getRollBackSql(dbInfoVo);
}
public static List<String> getRollBackSql(DbInfoVo dbInfoVo) throws Exception {
final OpenReplicator or = new OpenReplicator();
or.setUser(dbInfoVo.getUsername());
or.setPassword(dbInfoVo.getPassword());
or.setHost(dbInfoVo.getHost());
or.setPort(dbInfoVo.getPort());
or.setServerId(1);
// or.setBinlogPosition(4);
// or.setbinlo(4);
// or.setBinlogFileName("mysql-bin.000001");
or.setBinlogFileName(getFirstBinLogName(or));
boolean isTurn = true; //是否反sql
MyBinlogParser parser = new MyBinlogParser();
or.start(transport -> {
or.setBinlogEventListener(parser.getBinlogEventListener(isTurn));
parser.setTransport(transport);
parser.setBinlogFileName(or.getBinlogFileName());
parser.setBinlogFileName(or.getBinlogFileName());
parser.registerHandle(new InsertHandle(), WRITE_ROWS_EVENT, WRITE_ROWS_EVENT_V2);
parser.registerHandle(new DeleteHandle(), DELETE_ROWS_EVENT, DELETE_ROWS_EVENT_V2);
parser.registerHandle(new UpdateHandle(), UPDATE_ROWS_EVENT, UPDATE_ROWS_EVENT_V2);
parser.registerHandle(new TableMapHandle(dbInfoVo), TABLE_MAP_EVENT);
return parser;
});
return parser.getSqls();
}
}