2017-02-23 42 views
0

我MapReduce的工作與WORDCOUNT示例的值: 輸入數據:輸出減少一個密鑰一起

text files 

輸出:

term: fileName occurrences 

地圖輸出:

Term:filename 1 1 1 1 1 

減少輸出:

代碼最終輸出「減速機的輸出」的3210
Term: filename occurences 

例子:

Iphone: file1 4 
Iphone: file2 3 
Galaxy: file1 2 
Htc: file1 3 
Htc file2 5 

輸出我想

Iphone: file1=4 file2=3 
Galaxy: file1=2 
Htc: file1=3 file2=5 

我怎樣才能得到這種情況下,我想過使用分區功能,把我不知道該怎麼做?任何建議? 在此先感謝

+0

代碼輸出示例 - 是您的映射器輸出? –

+0

@siddharthajain從減速機輸出「最終輸出」 – user5532529

+0

你在地圖輸出中的關鍵是什麼,它的格式是什麼? –

回答

0

有很多種方法可以實現您想要的輸出,但由於您已經提到過要使用分區程序來做到這一點,所以我們可以這樣做。

根據你的問題,你需要在鍵上創建一個分區器,在這個分區器上你要劃分「Term」(iphone,Galaxy等)輸出。我假設你的地圖輸出鍵格式和值格式是文字,如果不作相應的更改。這是你的分區應該是什麼樣子

public class Partitioners extends org.apache.hadoop.mapreduce.Partitioner<Text,Text>{ 
// I have the written the code if there are 3 reducer(since you have 3 type of key). 
//Tip: your number of reducers should be equal to the no of batches you want to divide your map output into. 
    @Override 
    public int getPartition(Text key, Text value, int numReduceTasks) { 
       String Skey = key.toString(); 
     //Again make changes according to your requirement here but I think it will work according to the composite key you have mentioned 
     String term = Skey.substring(0, Skey.indexOf(':')); 
     if(term.equals("Iphone")) 
     { // this will send all the key having iphone in reducer 1 
      return 0; 
     }else if(term.equals("Galaxy")) 
     { // this will send all the key having Galaxy in reducer 2 
      return 1; 
     } 
     else{ 
      // this will send all the key having other then Iphone and galaxy which is Htc in your case in reducer 3 
      return 2; 
     } 
    } 
} 

現在,一旦分區完成後,我們需要這樣告訴我們的驅動程序類。關於這個附加按照您的驅動程序類

job.setPartitionerClass(Partitioners.class); 
job.setNumReduceTasks(3); //since we want 3 reducers 

這將劃分地圖輸出3分區器,現在您可以在減速器類中相應地減少輸出。

我希望這可以解決您的問題。如果不讓我知道。

+0

非常感謝你siddhartha的回答,非常感謝。在我的情況下,我有很多文件,並在這些文件中有很多術語,所以我認爲分區的想法不符合我的情況,因爲我會爲所有術語做很多「if語句」。我嘗試了上面提到的方法,但在添加的分區類中出現異常,例如「java.lang.RuntimeException:java.lang.NoSuchMethodException」。請,你能告訴我另一種獲得這種輸出的方法嗎?我嘗試寫StringBuilder,並將值追加到它,但我沒有得到我想要的輸出,可能是因爲鍵是複合? – user5532529

+0

你能告訴我更多關於錯誤日誌的信息,你可以嘗試或者你可以改變結構。的。複合鍵 –

+0

你說我可以在沒有分區功能的情況下做什麼,因爲我無法處理我在分區中有的所有術語。這是錯誤:java.lang.RuntimeException:java.lang.NoSuchMethodException:org.apache.hadoop.h.Driver $ Partitioners。() \t at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:115) \t at org.apache.hadoop.mapred.MapTask $ NewOutputCollector。 (MapTask.java:527) \t在org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:613) \t在org.apache.hadoop.mapred.MapTask.run(MapTask.java:305) – user5532529