天天看點

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是一緻的,是以源碼中不應該添加該行。

繼續閱讀