仿分词统计的MapReduce 程序。

HDFS 数据格式 : 
举例单条数据:02-26 08:01:56 [qtp512249001-42] INFO  async-statistics - class com.spring.aop.StorageManagerStatAspect${"method":"com.systoon.scloud.master.controller.ImageController.download","ip":"172.28.6.131","port":"38001","father":"sun.reflect.GeneratedMethodAccessor8.invoke/null/-1","requestIp":"106.39.33.246","argsMap":{"org.eclipse.jetty.server.Request:0":{"requestURI":"/f/KZ0wxxbvFz924VaHS8JN1Fk42jV9OBMCHYoLtuc9sAkfF.jpg"},"org.eclipse.jetty.server.Response:1":1462183982},"processTime":50,"time":1456444916225,"retValMap":{":":"this object is null"}}

是写出的一行日志。  日志结构是时间 + 打印的类 + JSON
那么现在是要进行一个统计 MR 分析。

那么开始上代码:
        
  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.rocky.util.TimeUtils;
  4. import org.apache.hadoop.fs.FileSystem;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.IntWritable;
  7. import org.apache.hadoop.io.LongWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapred.*;
  10. import org.apache.hadoop.mapred.lib.MultipleOutputFormat;
  11. import org.apache.hadoop.util.Progressable;
  12. import java.io.IOException;
  13. import java.net.URI;
  14. import java.util.Iterator;
  15. public class MulOutput {
  16. public static final String clazz = "com.spring.aop.StorageManagerStatAspect";
  17. public static final String m_download = "com.systoon.scloud.master.controller.ImageController.download";
  18. public static final String m_upload = "com.systoon.scloud.master.controller.DirectUploadFile.directUploadFile";
  19. public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable>{
  20. private final static IntWritable one = new IntWritable(1);
  21. Text word = new Text();
  22. @Override
  23. public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output,
  24. Reporter reporter) throws IOException {
  25. String line = value.toString();
  26. if(line.contains(clazz)){
  27. if(line.contains(m_download)){
  28. String tempObject = line.split(clazz)[1];
  29. String tmp = tempObject.substring(1,tempObject.length());
  30. JSONObject jsonObject = JSON.parseObject(tmp);
  31. String method = jsonObject.get("method").toString();
  32. if( method.equals(m_download) ){
  33. word.set("download");
  34. output.collect(word, one);
  35. }
  36. } else if(line.contains(m_upload)) {
  37. String tempObject = line.split(clazz)[1];
  38. String tmp = tempObject.substring(1,tempObject.length());
  39. JSONObject jsonObject = JSON.parseObject(tmp);
  40. String method = jsonObject.get("method").toString();
  41. if( method.equals(m_upload) ){
  42. word.set("upload");
  43. output.collect(word, one);
  44. }
  45. } else {
  46. word.set("debug");
  47. output.collect(word,one);
  48. }
  49. } else {
  50. word.set("others");
  51. output.collect(word, one);
  52. }
  53. }
  54. }
  55. public static class Reduce extends MapReduceBase
  56. implements Reducer<Text, IntWritable, Text, IntWritable> {
  57. public void reduce(Text key, Iterator<IntWritable> values,
  58. OutputCollector<Text, IntWritable> output, Reporter reporter)
  59. throws IOException{
  60. int sum = 0;
  61. while (values.hasNext()) {
  62. sum += values.next().get();
  63. }
  64. output.collect(key, new IntWritable(sum));
  65. }
  66. }
  67. public static void main(String[] args) throws Exception{
  68. JobConf jobConf = new JobConf(MulOutput.class);
  69. jobConf.setJobName("rocky_test");
  70. String outPath = "/test/mapReduce/statis"+TimeUtils.getStringDate();
  71. final FileSystem filesystem = FileSystem.get(new URI(outPath), jobConf);
  72. if(filesystem.exists(new Path(outPath))){
  73. filesystem.delete(new Path(outPath), true);
  74. }
  75. jobConf.setMapperClass(Map.class); //为job设置Mapper类
  76. jobConf.setMapOutputKeyClass(Text.class); //输出数据设置Key类
  77. jobConf.setMapOutputValueClass(IntWritable.class); //输出数据设置Key类
  78. jobConf.setCombinerClass(Reduce.class); // 为job设置Combiner类
  79. jobConf.setReducerClass(Reduce.class); // 为job设置Reduce类
  80. jobConf.setOutputKeyClass(Text.class); // 输出数据设置Key类
  81. jobConf.setOutputValueClass(IntWritable.class); // 输出数据设置Key类
  82. FileInputFormat.setInputPaths(jobConf, new Path("/test/mapReduce/statistics.log.2016-02-26"));
  83. // // 扫描组合path
  84. // FileInputFormat.addInputPath();
  85. jobConf.setOutputFormat(MyMultipleFilesTextOutputFormat.class);
  86. FileOutputFormat.setOutputPath(jobConf, new Path(outPath));
  87. JobClient.runJob(jobConf); //运行一个job
  88. }
  89. }


简单来讲就是 Map 按行读, Reduce 进行汇总。   也是统计中最最常用的。  轻松解决问题。













附件列表

     

    posted @ 2016-03-09 15:33  rocky_24  阅读(741)  评论(0编辑  收藏  举报
    希望祖国繁荣,富强! God has given me a gift. Only one. I am the most complete fighter in the world. My whole life, I have trained. I must prove I am worthy of someting. rocky_24