如何在mapreduce中对Map端程序的输出进行排序?

fjnneemd  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(295)

我的问题是如何在mapreduce程序中对Map器的输出进行排序(ps:there's no reducers(0)),我只使用Map端过滤两个输入,我希望结果(输出Map器)将按每个Map器的键进行排序。我怎样才能在不使用额外工作的情况下在同一份工作中完成这类工作?请您提出建议

gtlvzcf8

gtlvzcf81#

一个目录中有三个文件-在一个作业中由一个Map器排序。运行方式 hadoop jar sort.jar sort file:///path/sortFiles/ sortedFiles ```
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class sort{
public static class sortMapper extends Mapper<Object, Text, Text, Text> {
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

        //add filter logic here
        context.write(new Text(value), new Text(""));
    }
}

public static void main(String[] args) throws Exception {

      if(args.length != 2)
      {
          System.out.println("missing agrs: usage <prog> <arg1> <arg2>");
          System.exit(1);
      }
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "sort mutilple files");
    job.setJarByClass(sort.class);
    job.setMapperClass(sortMapper.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

    job.waitForCompletion(true);
  }

}

![](https://i.stack.imgur.com/Z51yD.png)
x4shl7ld

x4shl7ld2#

通过将所有预期结果收集到Map器上的本地/内存数据结构中,可以实现部分(每个Map器)排序。然后对其进行排序,最后运行 collector.write 对于现在排序的集合的所有元素。
因此,这里与普通行为的区别在于,在后一种情况下,每个元素只是在遇到它们时发出,从而产生随机/非有序输出。
请注意,结果仍然没有 total ordering :这将需要减速步骤。

相关问题