[Hadoop]

  在Hadoop的MR程序开发中,经常需要统计一些map/reduce的运行状态信息,这个时候我们可以通过自定义Counter来实现,这个实现的方式是不是通过配置信息完成的,而是通过代码运行时检查完成的。

  1、创建一个自己的Counter枚举类。

enum PROCESS_COUNTER {
    BAD_RECORDS,
    BAD_GROUPS;
}

  2、在需要统计的地方,比如map或者reduce阶段进行下列操作。

context.getCounter(PROCESS_COUNTER.BAD_RECORDS).increment(1); // 增加1
context.getCounter(PROCESS_COUNTER.BAD_RECORDS).increment(-1); // 减少1

  3、在job运行完成后获取统计信息,代码如下:

org.apache.hadoop.mapreduce.Counters counters = job.getCounters();
org.apache.hadoop.mapreduce.Counter counter = counters.findCounter(PROCESS_COUNTER.BAD_RECORDS);
System.out.println("bad records:" + counter.getValue());
System.out.println("bad groups:" + job.getCounters().findCounter(PROCESS_COUNTER.BAD_GROUPS).getValue());
原文地址:https://www.cnblogs.com/liuming1992/p/4829535.html