-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSQLFlashBackTool.java
More file actions
167 lines (148 loc) · 5.72 KB
/
Copy pathSQLFlashBackTool.java
File metadata and controls
167 lines (148 loc) · 5.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Copyright (c) 2013-Present, Alibaba Group Holding Limited.
* All rights reserved.
* <p>
* Licensed under the Server Side Public License v1 (SSPLv1).
*/
package com.aliyun.polardbx.cdc.tool;
import com.aliyun.polardbx.binlog.SpringContextBootStrap;
import com.aliyun.polardbx.binlog.SpringContextHolder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* created by ziyang.lb
* 解析binlog,并生成SQL,WIP
**/
@Slf4j
public class SQLFlashBackTool {
private static final Map<String, StringBuilder> fileMap = new HashMap<>();
@SneakyThrows
public static void main(String[] args) throws IOException, SQLException {
SpringContextBootStrap appContextBootStrap = new SpringContextBootStrap("testing-conf/spring-test.xml");
appContextBootStrap.boot();
DataSource dataSource = SpringContextHolder.getObject("h2DataSource");
//Set<String> set = getTableList("/Users/lubiao/Util/table_id_5654.log");
//print(set);
/*Set<String> set1 = getTableList("/Users/lubiao/Util/table_id_1500.log");
Set<String> set2 = getTableList("/Users/lubiao/Util/table_id_1501.log");
set2.addAll(set1);
print(set2);*/
}
private static void generateSql(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String s;
String tableName = "";
int pos = 0;
boolean insertFlag = false;
boolean updateFlag = false;
boolean updateBegin = false;
boolean updateEnd;
boolean deleteFlag = false;
boolean deleteBegin;
boolean deleteEnd;
while ((s = bufferedReader.readLine()) != null) {
if (StringUtils.startsWithIgnoreCase(s, "### INSERT INTO")) {
insertFlag = true;
tableName = StringUtils.substringAfter(s, "### INSERT INTO ");
fileMap.putIfAbsent(tableName, new StringBuilder());
fileMap.get(tableName).append("\n").append("INSERT INTO ").append(tableName).append("SELECT ");
} else if (StringUtils.startsWithIgnoreCase(s, "### UPDATE")) {
updateFlag = true;
} else if (StringUtils.startsWithIgnoreCase(s, "### DELETE FROM")) {
deleteFlag = true;
} else {
if (insertFlag) {
if (pos == 0) {
pos++;
} else {
if (StringUtils.startsWithIgnoreCase(s, "### @")) {
appendOneColumn(pos, tableName, s);
pos++;
} else {
fileMap.get(tableName).append(");");
insertFlag = false;
pos = 0;
}
}
} else if (updateFlag) {
if (updateBegin) {
if (StringUtils.startsWithIgnoreCase(s, "### @")) {
appendOneColumn(pos, tableName, s);
pos++;
} else {
fileMap.get(tableName).append(");");
updateFlag = false;
updateBegin = false;
pos = 0;
}
} else {
if (StringUtils.startsWithIgnoreCase(s, "### SET")) {
updateBegin = true;
}
}
} else if (deleteFlag) {
} else {
}
}
}
}
private static void appendOneColumn(int pos, String tableName, String s) {
if (pos != 1) {
fileMap.get(tableName).append(",");
}
s = StringUtils.substringAfter(s, "=");
s = StringUtils.substringBefore(s, "/*").trim();
fileMap.get(tableName).append(s).append(",");
}
private static void print(Set<String> set) {
for (String s1 : set) {
String[] array = StringUtils.split(s1, ".");
if ("`mysql`".equalsIgnoreCase(array[0])) {
continue;
}
if (StringUtils.startsWithIgnoreCase(array[0], "`__cdc_")) {
continue;
}
if (StringUtils.startsWithIgnoreCase(array[1], "`g_i_")) {
continue;
}
if ("`__drds_global_tx_log`".equalsIgnoreCase(array[1])) {
continue;
}
System.out.println(s1);
}
}
private static Set<String> getTableList(String fileStr) throws IOException {
// read target file
File file = new File(fileStr);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String s;
Set<String> set = new TreeSet<>();
while ((s = bufferedReader.readLine()) != null) {
String originStr = s;
try {
if (StringUtils.contains(s, "INSERT") || StringUtils.contains(s, "UPDATE") || StringUtils
.contains(s, "DELETE")) {
s = StringUtils.substringAfter(s, " `");
s = "`" + s;
set.add(s);
}
} catch (Throwable t) {
System.err.println(originStr);
}
}
return set;
}
}