作用: 用来统计altas某个关键字出现的字次,作为语句出现频率的一个统计,可以发现用户或是系统执行频繁的SQL或是其他的
一些文件处理
比如
比如 1,查找 sql_test.log这个文件中,以select开始后的 20个字符串,并进行分组
F:\idea\temp>java FileProcess sql_test.log "select " 20
select current_date( is 23
select TABLE_NAME
is 1
select (10416-9861)/ is 2
select (11010-1045 is 5
select * from a LIMI is 9
select * from db1.t1 is 24
2,我对select * from db1.t1这条语句比较感兴趣,并想得到更加明细的汇总
F:\idea\temp>java FileProcess sql_test.log "select * from db1.t1 " 30
select * from db1.t1 where id= is 1
select * from db1.t1 LIMIT 0, is 19
select * from db1.t1 for updat is 3
这样就得到了当前以select * from db1.t1开始的的语句明细
代码实现:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
public class FileProcess {
public static void main(String [] args) throws Exception {
String line;
String key;
Integer count;
String linelen=null;
HashMap<String,Integer> map=new HashMap<String, Integer>();
//first parameter is filename
File file=new File(args[0]);
FileInputStream fs=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fs);
BufferedReader br=new BufferedReader(isr);
line=br.readLine();
//default length of string
Integer stringlength=20;
//if user has input ,set stringleng to input
if (args.length==3) {
stringlength=Integer.parseInt(args[2]);
}
//begin loop
while(line!=null) {
//search first
if (line.indexOf(args[1])>1) {
//find
linelen=line.substring(line.indexOf(args[1]),line.length());
if (linelen.length()>stringlength) {
//substring as hashmap key
key=linelen.substring(0,stringlength);
//if the key exists,count++
if (map.get(key)!=null) {
count=map.get(key);
count=count+1;
map.put(key,count);
}
else
map.put(key,1);
}
}
line=br.readLine();
}
//output the content of hashmap
Iterator it=map.keySet().iterator();
while(it.hasNext()) {
String keystring=(String)it.next();
System.out.println(keystring+" is "+map.get(keystring));
}
}
}
github地址https://github.com/oraclefrankzou/test