[]hadoopカスタムCounter

1201 ワード

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