使用Mapreduce在hadoop中实现订单

时间:2014-12-23 10:06:31

标签: hadoop mapreduce

我想在Map Reduce程序中通过like子句实现order。

我的输入看起来像这样

1,Subhradip Bose,1

2,Prajakta Bose,2

2,Anup Singh,3

3,Mahesh Gurav,4

我写了一个类似于

的地图功能
protected void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {
    String[] currentLine = value.toString().split(",");
    context.write(new Text(currentLine[1]), new Text(currentLine[1]));
}

但我得到像这样的输出

Subhradip Bose

Prajakta Bose

Anup Singh

Mahesh Gurav

我想要输出如下

Anup Singh

Mahesh Gurav

Prajakta Bose 

Subhradip Bose

1 个答案:

答案 0 :(得分:0)

我认为你没有使用reducer,而mapper输出是最终输出

如果你引入减速器,则会发生改组和排序,你会获得所需的输出。

请参阅以下问题what-is-the-purpose-of-shuffling-and-sorting-phase-in-the-reducer

示例Reducer实现:

public class Reduce     
 extends Reducer<Text, Text,Text, Text> {


public void reduce(Text key, Iterable<Text> values, Context context) 
 throws IOException,InterruptedException {

  Iterator<Text> it = values.iterator();
  if(it.hasNext()){ // you can use loop here
      Text name = it.next();
      context.write(key, name);
  }

 }

}