2017-02-23 40 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