[]hadoop自定义Counter

hadoop0.20.X版本中对counter进行了改进,具体写法如下,mark一下

public static class TokenizerMapper 
       extends Mapper<Object, Text, Text, IntWritable>{
    
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
    private final static Logger log = Logger.getLogger(TokenizerMapper.class);
    private static Counter ct = null;
    
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
    	  String name = itr.nextToken();
    	  if("test".equals(name)){
    		  ct = context.getCounter("TestFinder", "发现test");
    		  ct.increment(1);
    	  }
        word.set(name);
        context.write(word, one);
      }
    }
  }

 直接定义即可,如无此counter,hadoop会自动添加此counter.

 

转自:http://aronlulu.iteye.com/blog/980312

你可能感兴趣的:(hadoop)