由于某某的不小心,把生产库给清了,数据没法恢复,情急之下分析日志填充数据,结果本人写了一个日志分析程序,代码如下:
package com.zby.exercise;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
/**
* @author :zoboy
* @Description:
* @ Date: Created in 2018-08-21 10:15
*/
public class Demo {
public static void main(String[] args) {
// String pattern = "insert into";
// String table="t_order_info";
String fileName = "D://console23-1842_1427.txt";
// String fileName = "D://console9096.txt";
String pattern = "UPDATE";
String table = "t_order_info";
updateSqlHandle(fileName, pattern, table);
}
public static void updateSqlHandle(String file, String pattern, String table) {
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
if (Pattern.matches(".*UPDATE t_ticket_info.*", str)) {
String sql = str.substring(str.indexOf(pattern), str.length());
// System.out.println(sql);
String pram = in.readLine();
String params = pram.substring(pram.indexOf("Parameters:") + 11, pram.length());
// System.out.println(params);
updateParamHandle(sql, params);
}
}
} catch (IOException e) {
}
}
public static void updateParamHandle(String sql, String pram) {
String[] prams = pram.split(",");
String[] pp = new String[prams.length];
int index = 0;
for (String str1 : prams) {
if (str1==null || str1.equals(" null")) {
pp[index] = str1;
} else if (Pattern.matches(".*BigDecimal.*", str1) || Pattern.matches(".*Integer.*", str1)) {
str1 = str1.substring(0, str1.indexOf("(")) + ",";
} else {
str1 = "'" + str1.substring(1, str1.indexOf("(")) + "'" + ",";
}
pp[index] = str1;
index++;
}
String[] asql = sql.split("\\?");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < asql.length - 1; i++) {
asql[i] += pp[i];
stringBuilder.append(asql[i].substring(0, asql[i].length() - 1));
}
System.out.println(stringBuilder.toString() + ";");
}
public static void insertSqlHandle(String file, String pattern, String table) {
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
if (Pattern.matches(".*" + pattern + " " + table + ".*", str)) {
String pram = in.readLine();
String params = pram.substring(pram.indexOf("Parameters:") + 11, pram.length());
paramHandle(str.substring(str.indexOf(pattern), str.length()), params);
}
}
} catch (IOException e) {
}
}
public static void paramHandle(String sql, String pram) {
String sqlResult = sql.substring(0, sql.indexOf("( ?")) + " (";
String[] prams = pram.split(",");
StringBuilder stringBuilder = new StringBuilder();
for (String str1 : prams) {
if (Pattern.matches(".*BigDecimal.*", str1) || Pattern.matches(".*Integer.*", str1)) {
str1 = str1.substring(0, str1.indexOf("(")) + ",";
} else {
str1 = "'" + str1.substring(1, str1.indexOf("(")) + "'" + ",";
}
stringBuilder.append(str1);
}
sqlResult += stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1) + ");";
System.out.println(sqlResult);
}
}
主要分析insert和update,delete的没有写,不过也很简单。执行之后直接生产sql代码,复制到数据库一执行数据就回来了:
效果如下: