天天看点

Map/Reduce运行时做了两次reduce的问题

问题是这样的,我在做抓取的文件中的URL统计时碰上了这么个问题:

       我的map方法中的output格式是这样的<Text, Text>,其中key是做了URLEncode的url,value是其他信息(格式为:爬虫名-html大小-抓取时间戳);

       我的reduce方法中是对URL的value做了下统计,统计完后在的output格式认为<Text, Text>,key仍然是做了URLEncode的url,value则是在原下的格式后加了个标记,代表抓取次数(格式为:爬虫名-html大小-抓取时间戳-抓取次数,即在原先的value后加了个: -抓取次数)。

       我的conf设置的属性有以下几个:

       //设置输出的key, value类型

        conf.setOutputKeyClass(Text.class);

        conf.setOutputValueClass(Text.class);

        //设置起始,结束标记

        conf.set("xmlinput.start", Config.RawPageStartKey);

        conf.set("xmlinput.end", Config.RawPageEndKey);

        conf.setJobName("URLInfo-Test");

        conf.setJarByClass(URLInfoTest.class);

        conf.setMapperClass(URLMapperTest.class);

        conf.setCombinerClass(URLReducerTest.class);

        conf.setReducerClass(URLReducerTest.class);

        conf.setInputFormat(XmlInputFormat.class);

        conf.setOutputFormat(TextOutputFormat.class);

       写完后,我打了个包放到集群上去运行,期望的输出结果是:http%3A%2F%2F0qiang0.blog.163.com%2Fmusicalbum    tsinghua.crawlers.spider.HtmlunitCrawler-25121-1335888626818-1

       可是现实的输出却是:http%3A%2F%2F0qiang0.blog.163.com%2Fmusicalbum    tsinghua.crawlers.spider.HtmlunitCrawler-25121-1335888626818-1-1

       也就是value中的抓取次数添加了两次,也就出现了输出中的两个 -1 ,我将reduce中的value打出来看了下,是正确的,可是为什么会出现两个 -1 呢?

       在郝总的帮助下,我找到了原因。原来是我的conf中设置了 conf.setCombinerClass(URLReducerTest.class) ,我进到该方法中看了下注释,里面有一句:Typically the combiner is same as the <code>Reducer</code> for the job,我们可以理解为他的功能和Reducer是一致的,所以源码中不应该添加该行。