1、定义计数器
1)枚举声明计数器
Context context...//自定义枚举变量EnumCounter counter = context.getCounter(Enum enum)
2)自定义计数器
Context context...//自己命名groupName和counterNameCounter counter = context.getCounter(String groupName,String counterName)
2、为计数器赋值
1)初始化计数器
counter.setValue(long value);//设置初始值
2)计数器自增
counter.increment(long incr);//增加计数
3、获取计数器的值
1) 获取枚举计数器的值
Job job...job.waitForCompletion(true);Counters counters=job.getCounters();Counter counter=counters.findCounter("BAD_RECORDS");//查找枚举计数器,假如Enum的变量为BAD_RECORDSlong value=counter.getValue();//获取计数值
2) 获取自定义计数器的值
Job job...job.waitForCompletion(true);Counters counters=job.getCounters();Counter counter=counters.findCounter("ErrorCounter","toolong");//假如groupName为ErrorCounter,counterName为toolonglong value=counter.getValue();//获取计数值
3) 获取内置计数器的值
textpop-up
Job job...job.waitForCompletion(true);Counters counters=job.getCounters();//查找作业运行启动的reduce个数的计数器,groupName和counterName可以从内置计数器表格查询(前面已经列举有)Counter counter=counters.findCounter("org.apache.hadoop.mapreduce.JobCounter","TOTAL_LAUNCHED_REDUCES");//假如groupName为org.apache.hadoop.mapreduce.JobCounter,counterName为TOTAL_LAUNCHED_REDUCESlong value=counter.getValue();//获取计数值
4) 获取所有计数器的值
Counters counters = job.getCounters();for (CounterGroup group : counters) { for (Counter counter : group) { System.out.println(counter.getDisplayName() + ": " + counter.getName() + ": "+ counter.getValue()); } }