太阳雨

痛并快乐着

BlogJava 首页 新随笔 联系 聚合 管理
  67 Posts :: 3 Stories :: 33 Comments :: 0 Trackbacks
在前面的废话不想看的可以skip-----由于工作需要,经常需要打开和查询250M左右的文本文件(总行数超过250万行)分析日志,公司发的笔记本内存才512M,地球人都知道,这样的电脑根本不能胜任。不知道有没有人和我一样倒霉,面对这么庞大的文件,曾经让我很无奈,很彷徨。从寻找优秀的类似notepad这样的文本工具,企图通过工具来提高下速度,其中JIURL这个记事本工具,我个人觉得很好,它陪我走了好长时间,虽然它老是有这样那样的BUG,不过它的速度确实比其他的记事本快很多,对于巨型文件,不会像其他记事本那样直接挂死。工具再好,也是死的,经常操作一样的动作,让我想撞墙。偶又去研究了Linux下的sh命令,哎,不是我说MicroSoft的系统不好,Linux的命令确实好使,特别是查询文档,日志分析的时候,只要你使用的好,基本都能完成你的任务。像grep,gawk,wc,head,hail etc.这样的命令总是能给我们带来惊喜。Linux系统还是要好好学的,虽然和widows差别很大,但是很强,很方便。 下面我就来说说用JAVASE怎么操作这些庞然大物的。 先介绍下我要操作的文本文件 [28-03-2008 00:00:00.101] |SH1|CIC015|->|SH1|AG015| >>IAM|CN15|CD51001500|CG15921351879|PA0|PB1|<< [28-03-2008 00:00:00.101] |SH1|CIC153|->|SH1|AG144| >>IAM|CN144|CD51001500|CG13761957943|PA0|PB1|<< [28-03-2008 00:00:00.117] |SH1|CIC015|<-|SH1|AG015| >>ACM|CN15|<< [28-03-2008 00:00:00.117] |SH1|CIC153|<-|SH1|AG144| >>ACM|CN144|<< [28-03-2008 00:00:00.179] |SH1|CIC111|->|SH1|AG105| >>REL|CN105|PA16|PB0|PC4|<< [28-03-2008 00:00:00.179] |SH1|CIC111|<-|SH1|AG105| >>RLC|CN105|<< [28-03-2008 00:00:00.273] |SH1|CIC1373|->|SH3|AG118| >>ANM|CN118|<< [28-03-2008 00:00:00.383] |SH1|CIC1365|<-|SH3|AG110| >>REL|CN110|PA16|<< [28-03-2008 00:00:00.461] |SH1|CIC1365|->|SH3|AG110| >>RLC|CN110|<< [28-03-2008 00:00:00.586] |SH1|CIC2577|->|S11|AG196| >>ANM|CN196|<< 大家不要头晕,这个本来就是日志,搞过No.7的人都看的懂,不过这个不是重点了。我们要处理就是这样的文件,JAVASE里有一个新的包 java.nio,具体干啥的,我就不说了,自己去查看API吧。下面贴出具体代码,大家重点看getResult() 方法。在里面使用了MappedByteBuffer把文件映射到内存里面去,然后使用缓存的技术把他再读出来,如果不适用缓存,速度会很慢,想在这个里面查个东西,至少要7分钟以上,使用了缓存整个文件遍历最多13秒,差距很大吧,这个时间还是和硬件有关系的,我的电脑比较烂了,我老婆经常说我的是“低廉的双核”,我那个汗啊!o(∩_∩)o...,大家好好看看里面的代码吧。希望能对经常和大型文件打交道的人有个帮助!

  1 package com.xhp.ss7 ;    
  2    
  3 import java.io.File ;    
  4 import java.io.RandomAccessFile ;    
  5 import java.io.BufferedReader ;    
  6 import java.io.StringReader ;    
  7 import java.util.List ;    
  8 import java.util.ArrayList ;    
  9 import java.util.Date ;    
 10 import java.util.regex.Pattern ;    
 11 import java.util.regex.Matcher ;    
 12 import java.nio.MappedByteBuffer ;    
 13 import java.nio.channels.FileChannel ;    
 14    
 15    
 16 public class SS7Handle{    
 17     private String year ;    
 18     private String month ;    
 19     private String day ;    
 20     private long length ;    
 21     private File file ;    
 22     private int bufferSize = 0x600000;// 默认6M的缓冲 ;    
 23     private long diff ;    
 24         
 25         
 26     public SS7Handle(String path){    
 27         this.loadFile(path) ;    
 28     }    
 29         
 30     public List<String> getResult(int type,String number,String time,String minute,String second,int count){    
 31         List<String> result = new ArrayList<String>() ;    
 32         String strRex = getStrRex(type,number,time,minute,second) ;    
 33         Pattern pattern = Pattern.compile(strRex) ;    
 34         int counts = count ;    
 35         Date begin = null ;    
 36         Date end = null ;    
 37         try{    
 38             MappedByteBuffer inputBuffer = new RandomAccessFile(file,"r").getChannel()    
 39                                 .map(FileChannel.MapMode.READ_ONLY, 0, length);    
 40             byte[] dst = new byte[bufferSize] ; //每次读出6M的内容     
 41             BufferedReader br = null ;    
 42             boolean findFirst  = false ;     
 43             boolean over = false ;    
 44             begin = new Date() ;            
 45             for(int offset=0; offset < length; offset+= bufferSize){    
 46                 //for(int offset=0; offset < 1; offset+= bufferSize){    
 47                         
 48                 if(length - offset >= bufferSize){    
 49                     for(int i = 0;i < bufferSize;i++)    
 50                               dst[i] = inputBuffer.get(offset + i);    
 51                 }else{    
 52                     for(int i = 0;i < length - offset;i++)    
 53                     dst[i] = inputBuffer.get(offset + i);    
 54                 }    
 55                 //把得到的dst 交给 BufferedReader 按行读出    
 56                 br = new BufferedReader(new StringReader(new String(dst))) ;    
 57                 String line ;    
 58                 StringBuffer next = new StringBuffer() ;    
 59    
 60                 while((line=br.readLine()) != null ){    
 61                     if(!findFirst){        
 62                         if(pattern.matcher(line).find()){    
 63                                 System.out.println("line-->"+line) ;    
 64                                 counts-- ;    
 65                                 result.add(line) ;    
 66                                 findFirst = true ;    
 67                                 String[] temp = line.split("\|") ;    
 68                                 next.append(temp[4]).append("|").append(temp[5]) ;    
 69                         }    
 70                     }else if(findFirst) {    
 71                         if(line.contains(next.toString())){    
 72                             System.out.println(next) ;    
 73                             result.add(line) ;    
 74                             counts-- ;    
 75                         }    
 76                     }    
 77                     if(counts<0){    
 78                         over = true ;    
 79                         break ;    
 80                     }     
 81                 }                
 82                 br.close() ;    
 83                 if(over){    
 84                     break ;    
 85                 }           
 86             }    
 87             end = new Date();    
 88             this.setDiff(end.getTime()-begin.getTime()) ;    
 89         }catch(Exception e){    
 90             System.out.println("error") ;    
 91         }    
 92         return result ;    
 93     }     
 94         
 95         
 96     private String getStrRex(int type,String number,String hour,String minute,String second){    
 97         if(0<minute.length() && minute.length()<2){    
 98             minute = "0"+minute ;    
 99         }else if(0<second.length() && second.length()<2){    
100             second = "0" +second ;    
101         }    
102         StringBuffer temp = new StringBuffer() ;    
103         if(type == 1){  // 查询 系统 打出的电话    
104             String outnumber = "CD"+number ;    
105             temp.append("^\[").append(day).append("\-").append(month).append("\-").append(year)    
106                             .append(" ").append(hour)    
107                             .append(!(minute.trim()==null || "".equals(minute.trim()))?("\:"+minute):"")    
108                             .append(!(second.trim()==null || "".equals(second.trim()))?("\:"+second):"")    
109                             .append(".*\<\-.*IAM.*").append(outnumber).append(".*") ;    
110             System.out.println(temp.toString()) ;     
111         }else if(type ==2){  //查询 用户打进的 电话    
112             String innumber = "CG" + number ;    
113             temp.append("^\[").append(day).append("\-").append(month).append("\-").append(year)    
114                             .append(" ").append(hour)    
115                             .append(!(minute.trim()==null || "".equals(minute.trim()))?("\:"+minute):"")    
116                             .append(!(second.trim()==null || "".equals(second.trim()))?("\:"+second):"")    
117                             .append(".*\-\>.*IAM.*").append(innumber).append(".*") ;     
118         }    
119         return temp.toString() ;    
120     }    
121         
122     public void loadFile(String path){    
123         this.file = new File(path) ;      
124         this.setLength(file.length()) ;    
125         int pathLength = path.length() ;    
126         int start = pathLength - new String("xxxxxxxx_xxx.xxx").length() ;    
127         this.setYear(path.substring(start,start+4)) ;    
128         this.setMonth(path.substring(start+4,start+6)) ;    
129         this.setDay(path.substring(start+6,start+8)) ;    
130     }    
131         
132     public void setYear(String year){    
133         this.year = year ;    
134     }    
135     public void setMonth(String month){    
136         this.month = month ;    
137     }    
138     public void setDay(String day){    
139         this.day = day ;    
140     }    
141     private void setLength(long length){    
142         this.length = length ;    
143     }    
144     public void setDiff(long diff){    
145         this.diff = diff ;    
146     }    
147     public long getDiff(){    
148         return this.diff ;    
149     }    
150     public long getLength(){    
151         return this.length ;    
152     }    
153     public String getYear(){    
154         return this.year ;    
155     }    
156     public String getMonth(){    
157         return this.month ;    
158     }    
159     public String getDay(){    
160         return this.day ;    
161     }    
162     public String toString(){    
163         return this.year+"-"+this.month+"-"+this.day ;    
164     }    
165    
166 
posted on 2008-11-02 14:51 小虫旺福 阅读(933) 评论(0)  编辑  收藏 所属分类: javaEE

只有注册用户登录后才能发表评论。


网站导航: