可以在程序中定义不同的计数器,分别累计特定事件的发生次数。对于同一作业所有任务的相同计数器,Hadoop会自动对他们求和,以反映整个作业的情况。
在Map以及reduce方法中,有一个reporter对象。例如以下代码红色字体部分:
public static class MapClass extends MapReduceBase
implements Mapper<Text, Text, IntWritable, IntWritable> {
private final static IntWritable uno = new IntWritable(1);
private IntWritable citationCount = new IntWritable();
public void map(Text key, Text value,
OutputCollector<IntWritable, IntWritable> output,
Reporter reporter) throws IOException {
try
{
citationCount.set(Integer.parseInt(value.toString()));
}
catch (NumberFormatException e)
{
citationCount.set(9999);
reporter.incrCounter("Mygrp","noint",1);
}
output.collect(citationCount, uno);
}
}
上述代码是对昨天CitationHistogram代码的修改,在捕获到异常,无法完成字符串到整型的转换时,定义计数器,Mygrp,noint。
在定义计数器的程序运行后,在web的监控页面中,通常地址是:http://hdpnamenode:19888/jobhistory/jobcounters/job_id
可以看到:
|