资源描述:
《Storm实时日志分析实战--编码实现》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Storm实时日志分析实战--编码实现LogParserBolt类packagecom.ibeifeng.bigdata.storm.weglog;importbacktype.storm.task.OutputCollector;importbacktype.storm.task.TopologyContext;importbacktype.storm.topology.IBasicBolt;importbacktype.storm.topology.IRichBolt;importbacktype.storm.topology.OutputFieldsDeclarer;i
2、mportbacktype.storm.tuple.Fields;importbacktype.storm.tuple.Tuple;importbacktype.storm.tuple.Values;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Map;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importstaticcom.ibeifeng.bigda
3、ta.storm.weglog.WebLogConstants.*;/***日志解析类*Createdbyadon2016/12/17.*/publicclassLogParserBoltimplementsIRichBolt{privatePatternpattern;privateOutputCollectorcollector;@Overridepublicvoidprepare(MapstormConf,TopologyContextcontext,OutputCollectorcollector){pattern=Pattern.compile("([^]*)[^]
4、*[^]*\[([\d+]*)\]\"[^]*([^]*)[^]*\"\d{3}\d+\"([^"]*)\"\"([^"]*)\"\"[^]*\"");this.collector=collector;}@Overridepublicvoidexecute(Tupleinput){StringwebLog=input.getStringByField("str");//解析if(webLog!=null
5、
6、!"".equals(webLog)){Matchermatcher=pattern.matcher(webLog);if(m
7、atcher.find()){//matcher.group(0);Stringip=matcher.group(1);StringserverTimeStr=matcher.group(2);//处理时间longtimestamp=Long.parseLong(serverTimeStr);Datedate=newDate();date.setTime(timestamp);DateFormatdf=newSimpleDateFormat("yyyyMMddHHmm");StringdateStr=df.format(date);Stringday=dateStr.subs
8、tring(0,8);Stringhour=dateStr.substring(0,10);Stringminute=dateStr;StringrequestUrl=matcher.group(3);StringhttpRefer=matcher.group(4);StringuserAgent=matcher.group(5);//分流this.collector.emit(IP_COUNT_STREAM,input,newValues(day,hour,minute,ip));this.collector.emit(URL_PARSER_STREAM,input,new
9、Values(day,hour,minute,requestUrl));this.collector.emit(HTTPREFER_PARSER_STREAM,input,newValues(day,hour,minute,httpRefer));this.collector.emit(USERAGENT_PARSER_STREAM,input,newValues(day,hour,minute,userAgent));}}this.collector.ack(input);}@Overridepubl