MapReduce计数器

来源:互联网 发布:ubuntu google输入法 编辑:程序博客网 时间:2024/06/05 20:16
(1)计数器主要用来收集系统信息,以及相关作业的运行时候的统计数据,用于知道作业成功、失败等情况;
(2)相比而言,计数器方式比日志更易于分析。


内置计数器:
(1)Hadoop内置的计数器,主要用来记录作业的执行情况
(2)内置计数器包括MapReduce框架计数器(Map-Reduce Framework)
    ——文件系统计数器(FielSystemCounters)
    ——作业计数器(Job Counters)
    ——文件输入格式计数器(File Output Format Counters)
    ——文件输出格式计数器(File Input Format Counters)
(3)计数器由相关的task进行维护,定期传递给tasktracker,再由tasktracker传给jobtracker;
(4)最终的作业计数器实际上是有jobtracker维护,所以计数器可以被全局汇总,同时也不必在整个网络中传递
(5)只有当一个作业执行成功后,最终的计数器的值才是完整可靠的;


自定义Java计数器:
(1)MapReduce允许用户自定义计数器;
(2)计数器是一个全局变量;
(3)计数器有组的概念,可以用Java的枚举类型或者用字符串来定义
方法:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. Counter getCounter(Enum<?> counterName) ;  
  2. Counter getCounter(String groupName,String counterName) ;  


(4)字符串方式(动态计数器)比枚举类型要更加灵活,可以动态在一个组下面添加多个计数器;
(5)在旧版API中使用Reporter,而新版API使用context.getCounter(groupName,counterName)来获取计数器配置并设置;
(6)计数器递增
方法:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. void increment(long incr) ;  
按给定的值增长计数器;
源代码org.apache.hadoop.mapreduce.Counter类。

计数器使用:
(1)WebUI 查看(50030,50070);
(2)命令行方式:hadoop job -counter;
(3)使用Hadoop API
通过job.getCounters()得到Counters,而后调用counters.findCounter()方法去得到计数器对象;

查看最终的计数器的值需要等作用完成后。

自定义计数器实验,统计词汇行中词汇数超过2个或少于2个的行数:
输入数据文件counter.txt:
hello world
hello
hello world 111
hello world 111 222

新建项目TestCounter,包com.counter,
源代码MyMapper.java:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.counter;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.io.LongWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Mapper;  
  8.   
  9. public class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text> {  
  10.   
  11.     @Override  
  12.     protected void map(LongWritable key, Text value,  
  13.             org.apache.hadoop.mapreduce.Mapper.Context context)  
  14.             throws IOException, InterruptedException {  
  15.         // TODO Auto-generated method stub  
  16.         String[] val = value.toString().split("\\s+");  
  17.         if(val.length < 2)  
  18.             context.getCounter("ErrorCounter","below_2").increment(1);  
  19.         else if(val.length > 2)  
  20.             context.getCounter("ErrorCounter""above_2").increment(1);  
  21.         context.write(key, value);  
  22.     }  
  23.   
  24. }  

源代码TestCounter.java:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.counter;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.mapreduce.Job;  
  8. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  10. import org.apache.hadoop.util.GenericOptionsParser;  
  11.   
  12.   
  13. public class TestCounter {  
  14.       
  15.       
  16.       
  17.   public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{  
  18.       Configuration conf = new Configuration();  
  19.         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  20.         if (otherArgs.length != 2) {  
  21.           System.err.println("Usage: wordcount <in> <out>");  
  22.           System.exit(2);  
  23.         }  
  24.         Job job = new Job(conf, "word count");  
  25.         job.setJarByClass(TestCounter.class);  
  26.         job.setMapperClass(MyMapper.class);  
  27.           
  28.         job.setNumReduceTasks(0);  
  29.           
  30.           
  31.           
  32.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  33.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  34.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  35.   }  
  36. }  




计数器统计结果:


先到这里吧。。。
0 0
原创粉丝点击