Resolved database table names in text logs, and automatic name mapping of table names

The development background of the tool: The
crm system is transformed based on ework. In the past, code and tables were mixed with ework for simplicity. Now it needs to be analyzed, and it needs to be re-analyzed. Which are shared and which are private.
Through the table names printed in the log, you can analyze which tables are unique to crm.
Determine which tables are new tables and few new tables through the creation records of pdm file tables, and then remove the unique ones of crm, so it is convenient to manually check which ones are exclusive to crm.

In addition, the tables used in the us system are analyzed (the ones used there must not be public), and the
cas, permissions, users, organizational units, and data dictionaries must be public.
In addition, considering the compromise and laziness in the previous development, some methods are mixed with ework and crm parties to use tables that do not need to be used.

General idea:
The log (or initialization sql script) or the table name list results analyzed in pdm are stored in the collection package respectively, and the collection operation and printing (print table name, code, and package in pdm) are done through CollectionTool.java inside. To facilitate manual judgment), save.

The tool is written in a hurry, and there may be a small number of bugs and inconsistent comments. If you don't understand, please study the code, and we will not provide technical support.

The tool is divided into 3 parts, which is the part where the log (or initialization sql script) parses the table name.
package chenxiaowen.tool.logs;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import chenxiaowen.tool.collections.CollectionTool;

/**
 * Parse the log file, print the table name<br>
 * Table names are uniformly lowercase
 *
 * @author Chen [email protected]
 *
 */
public class TablesInLogAnalysis {
	public TreeMap<String, Integer> tableTimes = new TreeMap<String, Integer>();
	public String encoding = "UTF-8";

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {
		TablesInLogAnalysis tool = new TablesInLogAnalysis();
		File baseLogFile = new File("D:\\tomcat\\logs");
		// File baseLogFile = new File("D:\\tomcat\\logs\\test.log");
		tool.analysisAndSaveResultToCollectionFile(baseLogFile, "c.crm.txt");
	}

	/**
	 * Parse the log in the directory where the log file is located or a single log file, and write the table name to the collection file<br>
	 * If the file is too large to cause memory overflow, please configure the running vm parameters, such as: -Xms400m -Xmx1024m
	 *
	 * @param logFile supports single file and directory
	 * @param recFileName
	 * @throws Exception
	 */
	public void analysisAndSaveResultToCollectionFile(File logFile,
			String recFileName) throws Exception {
		tableTimes.clear();
		analysisTablesInLogDir(logFile);
		StringBuffer b = new StringBuffer();
		for (Iterator iterator = tableTimes.keySet().iterator(); iterator
				.hasNext();) {
			String tbcode = (String) iterator.next();
			b.append(tbcode);
			b.append(",");
		}
		File recFile = new File(CollectionTool.getCollectionDir(), "c.ami.txt");
		if (recFile.exists()) {
			System.out.println("Delete existing log file" + recFile.getAbsolutePath());
			recFile.delete();
		}
		System.out.println("Save log file" + recFile.getAbsolutePath());
		System.out.println("表:");
		System.out.println(b.toString());
		FileUtils.writeStringToFile(recFile, b.toString(), encoding);
	}

	/**
	 * print the parsed table name
	 *
	 * @throws IOException
	 */
	public void printTables() throws IOException {
		for (Iterator iterator = tableTimes.keySet().iterator(); iterator
				.hasNext();) {
			String tbcode = (String) iterator.next();
			System.out.println(tbcode + ",");
		}
	}

	/**
	 * Parse directory
	 *
	 * @param f
	 * @throws IOException
	 */
	public void analysisTablesInLogDir(File f) throws Exception {
		if (f.isDirectory()) {
			File[] fs = f.listFiles();
			for (int i = 0; i < fs.length; i++) {
				analysisTablesInLogDir(fs[i]);
			}
		} else {
			System.out.println("Read parsing" + f.getAbsolutePath());
			String txt = getTextFormFile(f);
			addTableByAnalysis(txt);
		}
	}

	public String getTextFormFile(File f) throws IOException {
		String txt = FileUtils.readFileToString(f, encoding);
		return txt;
	}

	/**
	 * getTextFormFile("test.log")
	 *
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public String getTextFormFile(String filename) throws IOException {
		InputStream in = TablesInLogAnalysis.class
				.getResourceAsStream(filename);
		String txt = IOUtils.toString(in, encoding);
		return txt;
	}

	/**
	 * Analyze text content, add tables to records
	 *
	 * @param txt
	 * @throws IOException
	 */
	public void addTableByAnalysis(String txt) throws Exception {
		String regx = "(^|[\\s,])" // The separator in front of the table name: \s refers to a space, which may start directly with the table name, or a comma
				+ "((t_|rpt_|dic_)[a-z0-9_]+)"// table name (pdm is called code)
				+ ""// nothing after
		;
		int[] groupidxs = new int[] { 2 };
		ArrayList<String[]> findstrs = findAllWithGroup(txt, regx, groupidxs);
		for (String[] strs : findstrs) {
			String tbcode = strs[0].toLowerCase();// Uniform lowercase
			tbcode = TableNameProxy.getTranslateTableName(tbcode);// Table name encoding mapping, such as t_a_12
			// map t_a_01
			Integer times = tableTimes.get(tbcode);
			if (times == null) {
				tableTimes.put(tbcode, 1);
			} else {
				times++;
				tableTimes.put(tbcode, times);
			}
		}
	}

	/**
	 * Use regular expressions to find all matching content from the file, and return the string of the specified group after the match is successful
	 *
	 * @param txt
	 * @param regx
	 * @param groupidxs
	 * @return
	 * @throws IOException
	 */
	public static ArrayList<String[]> findAllWithGroup(String txt, String regx,
			int[] groupidxs) throws IOException {
		PatternCompiler compiler = new Perl5Compiler();
		Pattern pattern = null;
		ArrayList<String[]> findstrs = new ArrayList<String[]>();
		try {
			pattern = compiler.compile(regx,
					Perl5Compiler.CASE_INSENSITIVE_MASK);//Case insensitive
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(txt);
			while (matcher.contains(input, pattern)) {
				MatchResult result = matcher.getMatch ();
				String[] rst = new String[groupidxs.length];
				for (int i = 0; i < groupidxs.length; i++) {
					rst[i] = result.group(groupidxs[i]);
				}
				findstrs.add(rst);
			}
		} catch (MalformedPatternException e) {
			e.printStackTrace ();
		}

		return findstrs;
	}
}


package chenxiaowen.tool.logs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

/**
 * Mapping of special table names
 * @author Chen [email protected]
 *
 */
public class TableNameProxy {

	public static ArrayList<String[]> tableSpecialTranslate = new ArrayList<String[]>();
	// <regx,Pattern>
	public static HashMap<String, Pattern> patternsMap = new HashMap<String, Pattern>();
	// Regex compares batch objects
	public static PatternMatcher matcher = new Perl5Matcher();

	static {
		//TODO set here
		tableSpecialTranslate.add(new String[] { "^T_SET_LOG_\\d\\d$", "t_set_log_01,t_set_log_02" });
		tableSpecialTranslate.add(new String[] { "^T_SET_LOG_MM$", "t_set_log_01,t_set_log_02" });
		
		// used to define the regex object template type
		PatternCompiler compiler = new Perl5Compiler();
		for (Iterator iterator = tableSpecialTranslate.iterator(); iterator
				.hasNext();) {
			String[] setting = (String[]) iterator.next();
			String regstr = setting [0];
			try {
				// instance case-insensitive regex template
				Pattern pattern = compiler.compile(regstr,
						Perl5Compiler.CASE_INSENSITIVE_MASK/* If this parameter is removed, it is sensitive*/);
				patternsMap.put(regstr, pattern);
			} catch (MalformedPatternException e) {
				e.printStackTrace ();
			}
		}
	}

	/**
	 * @param args
	 * @throws MalformedPatternException
	 */
	public static void main(String[] args) throws MalformedPatternException {
		String tbstr = "t_1,t_1,t_set_log_02,t_set_log_01,t_set_log_03";
		String[] tbs = tbstr.split(",");
		HashSet<String> distinct = new HashSet<String>();
		
		for (int i = 0; i < tbs.length; i++) {
			String tb = tbs[i];
			tb = getTranslateTableName(tb);
			if(!distinct.contains(tb)){
				distinct.add(tb);
			}
		}

		for (Iterator iterator = distinct.iterator(); iterator.hasNext();) {
			String tb = (String) iterator.next();
			System.out.println(tb);
		}		
	}

	/**
	 * Table name matching translation
	 * @param tableName
	 * @return
	 * @throws MalformedPatternException
	 */
	public static String getTranslateTableName(String tableName)
			throws MalformedPatternException {
		for (Iterator iterator = tableSpecialTranslate.iterator(); iterator
				.hasNext();) {
			String[] setting = (String[]) iterator.next();
			String regstr = setting [0];
			Pattern pattern = patternsMap.get(regstr);
			if (matcher.contains(tableName, pattern)) {
				return setting[1].toLowerCase();
			}
		}

		return tableName;
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326441216&siteId=291194637