使用mapreduce在hadoop中实现order by

9nvpjoqh  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(363)

我想在map reduce程序中实现orderbylike子句。
我的输入是这样的

1,Subhradip Bose,1

2,Prajakta Bose,2

2,Anup Singh,3

3,Mahesh Gurav,4

我写了一个map函数

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
mwyxok5s

mwyxok5s1#

我认为您没有使用reducer,Map器输出是最终输出
如果引入减缩器,则会发生洗牌和排序,从而获得所需的输出。
请参考下面的问题在减速机中洗牌和排序阶段的目的是什么
简化程序实现示例:

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);
  }

 }

}

相关问题