统计某个字符串出现的频繁程度

作用: 用来统计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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值