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 进行汇总。   也是统计中最最常用的。  轻松解决问题。









附件列表

仿分词统计的MapReduce 程序。的更多相关文章

  1. 在Hadoop上运行基于RMM中文分词算法的MapReduce程序

    原文:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-count-on-hadoop/ 在Hadoop上运行基于RMM中文分词 ...

  2. 大数据学习——mapreduce程序单词统计

    项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  3. 从零开始学习Hadoop--第2章 第一个MapReduce程序

    1.Hadoop从头说 1.1 Google是一家做搜索的公司 做搜索是技术难度很高的活.首先要存储很多的数据,要把全球的大部分网页都抓下来,可想而知存储量有多大.然后,要能快速检索网页,用户输入几个 ...

  4. Eclipse下使用Hadoop单机模式调试MapReduce程序

    在单机模式下Hadoop不会使用HDFS,也不会开启任何Hadoop守护进程,所有程序将在一个JVM上运行并且最多只允许拥有一个reducer 在Eclipse中新创建一个hadoop-test的Ja ...

  5. 使用Python实现Hadoop MapReduce程序

    转自:使用Python实现Hadoop MapReduce程序 英文原文:Writing an Hadoop MapReduce Program in Python 根据上面两篇文章,下面是我在自己的 ...

  6. 用PHP编写Hadoop的MapReduce程序

    用PHP编写Hadoop的MapReduce程序     Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...

  7. HADOOP之MAPREDUCE程序应用二

    摘要:MapReduce程序进行单词计数. 关键词:MapReduce程序  单词计数 数据源:人工构造英文文档file1.txt,file2.txt. file1.txt 内容 Hello   Ha ...

  8. Hadoop之MapReduce程序应用三

    摘要:MapReduce程序进行数据去重. 关键词:MapReduce   数据去重 数据源:人工构造日志数据集log-file1.txt和log-file2.txt. log-file1.txt内容 ...

  9. 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行

    [TOC] 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行 程序源码 import java.io.IOException; import java.util. ...

随机推荐

  1. intellij idea 15 for mac破解版(含注册码)下载

    转载 https://blog.csdn.net/dataiyangu/article/details/81163118

  2. Windows server 2012R清除并重建SID 用于制作封装模板

    首先介绍下什么是SID SID也就是安全标识符(Security Identifiers),是标识用户.组和计算机帐户的唯一的号码.在第一次创建该帐户时,将给网络上的每一个帐户发布一个唯一的 SID. ...

  3. 注解@SuppressWarnings

    在JAVA中注解@SuppressWarnings("deprecation")的Deprecation是什么意思 过期的 @SuppressWarnings("depr ...

  4. 微信小程序 上传图片

    效果图  如上,js  如下,在页面循环图片就可以 /** * 选择图片 */ uploadImgAdd: function(e) { var imgs = this.data.imgs; wx.ch ...

  5. mysql死锁-查询锁表进程-分析锁表原因

    查询锁表进程: 1.查询是否锁表 show OPEN TABLES where In_use > 0;   2.查询进程     show processlist   查询到相对应的进程===然 ...

  6. 巧妇能为少米之炊(1)——Android下小内存下的生存之道

    常常听到身边用安卓的朋友抱怨手机卡顿,内存动不动就快没了.而Google声称在512M的内存下也能流畅执行Android 4.4.究竟它做了什么? 总结一下它主要做了四件事: 1.优化内核,使用Act ...

  7. HDU 4930 Fighting the Landlords(暴力枚举+模拟)

    HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型.假设先手能一步走完.或者一步让后手无法管上,就赢 思路:先枚举出两个人全部可能的牌型的最大值.然后再 ...

  8. codeforces@281 B

    shui #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  9. BZOJ4477: [Jsoi2015]字符串树

    [传送门:BZOJ4477] 简要题意: 给出一棵n个点的树,树上的边都代表一个字符串,给出Q个询问,每个询问输入x,y和字符串s,求出x到y的路径上以s为前缀的字符串个数 题解: 自己yy了一波可持 ...

  10. hdoj--2282--Chocolate(最小费用)

    Chocolate Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...