现在的位置: 首页 > 综合 > 正文

hadoop学习笔记–第九天–Report Counter

2014年02月27日 ⁄ 综合 ⁄ 共 817字 ⁄ 字号 评论关闭

    可以在程序中定义不同的计数器,分别累计特定事件的发生次数。对于同一作业所有任务的相同计数器,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

    可以看到:

Mygrp
Name
Map
Reduce
Total
noint 1 0 1

抱歉!评论已关闭.