手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop分析Tomcat访问日志

[日期:2012-01-27] 来源:Linux社区  作者:dsbjoe [字体:大 中 小]

照着《Hadoop实战》(见 http://www.linuxidc.com/Linux/2012-01/52481.htm )写了个小例子,解析tomcat日志统计各个浏览器的访问次数

  1. package com.ice.stat;   
  2.   
  3. import Java.io.IOException;   
  4. import java.util.regex.Matcher;   
  5. import java.util.regex.Pattern;   
  6.   
  7. import org.apache.hadoop.fs.Path;   
  8. import org.apache.hadoop.io.IntWritable;   
  9. import org.apache.hadoop.io.Text;   
  10. import org.apache.hadoop.mapreduce.Job;   
  11. import org.apache.hadoop.mapreduce.Mapper;   
  12. import org.apache.hadoop.mapreduce.Reducer;   
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;   
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;   
  15.   
  16. public class TomcatLog{   
  17.        
  18.     static class TomcatMapper extends Mapper<Object, Text, Text, IntWritable> {   
  19.   
  20.         private static final IntWritable one = new IntWritable(1);   
  21.         private static Pattern pattern = Pattern.compile("([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),(.*)");   
  22.            
  23.         protected void map(Object key, Text value,    
  24.                   Context context) throws IOException ,InterruptedException {   
  25.                
  26.             String line = value.toString();   
  27.             System.out.println(line);   
  28.             Matcher m = pattern.matcher(line);   
  29.             if(m.matches()){   
  30.                 String agent = m.group(9).toLowerCase();   
  31.                 if(agent.contains("chrome")){   
  32.                     agent = "chrome";   
  33.                 }else if(agent.contains("safari")){   
  34.                     agent = "safari";   
  35.                 }else if(agent.contains("firefox")){   
  36.                     agent = "firefox";   
  37.                 }else{   
  38.                     agent = "other";   
  39.                 }   
  40.                 Text t = new Text(agent);      
  41.                 context.write(t, one);   
  42.             }   
  43.         };   
  44.            
  45.     }   
  46.        
  47.     static class TomcatReducer extends Reducer<Text, IntWritable, Text, IntWritable>{   
  48.         protected void reduce(Text key, java.lang.Iterable<IntWritable> value, org.apache.hadoop.mapreduce.Reducer<Text,IntWritable,Text,IntWritable>.Context context) throws IOException ,InterruptedException {   
  49.             int count = 0;   
  50.             for(IntWritable v : value){   
  51.                 count = count + v.get();   
  52.             }   
  53.             context.write(key, new IntWritable(count));   
  54.         };   
  55.     }   
  56.        
  57.     public static void main(String[] args) throws Exception {   
  58.         if(args.length != 2){   
  59.             System.err.println("参数个数不对");   
  60.             System.exit(-1);   
  61.         }   
  62.         Job job = new Job();   
  63.         job.setJarByClass(TomcatLog.class);   
  64.            
  65.         FileInputFormat.addInputPath(job, new Path(args[0]));   
  66.         FileOutputFormat.setOutputPath(job, new Path(args[1]));   
  67.            
  68.         job.setMapperClass(TomcatMapper.class);   
  69.         job.setReducerClass(TomcatReducer.class);   
  70.         job.setOutputKeyClass(Text.class);   
  71.         job.setOutputValueClass(IntWritable.class);   
  72.         System.exit(job.waitForCompletion(true) ? 0 : 1);   
  73.            
  74.     }    
  75.   
  76. }  
linux
相关资讯       Tomcat  Hadoop 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款