一、hadoop有很多自带的计数器,相信看过运行log的都会看到各种数据
二、用户自定义计数器
在开发中经常需要记录错误的数据条数,就可以用计数器来解决。
1、定义:用一个枚举来定义一组计数器,枚举中的每个元素都是一个计数器
在main类中定义
enum RecordsCounter{
RIGHT_COUNTER,
WRONG_COUNTER
};
2、使用
在map和reduce端均可使用,job会在技术后收集数据。
在需要记录的地方:
context.getCounter(RecordsCounter.WRONG_COUNTER).increment(1);
在run函数中,在job执行完后得到结果:
Counters counters = job.getCounters();
Counter counter = counters.findCounter(RecordsCounter.WRONG_COUNTER);
Long wrongCount = counter.getValue();