mapreduce中文件的处理子集



我需要使用mapreduce处理一个巨大的文件,我需要一个方法来让最终用户选择他们想要处理多少条记录。

问题是没有任何有效的方法来处理文件的子集,而不"映射"整个文件(25tb文件)

是否有办法在特定记录数后停止映射并继续进行reduce部分?

这个问题有一个非常简单而优雅的解决方案:覆盖org.apache.hadoop.mapreduce.Mapper类的run(),只执行map(),直到你需要或只执行那些你需要/想要的记录。

参见以下内容:

public static class MapJob extends Mapper<LongWritable, Text, Text, Text> {
    private Text outputKey = new Text();
    private Text outputValue = new Text();
    private int numberOfRecordsToProcess;
    // read numberOfRecordsToProcess in setup method from the configuration values set in the driver class after getting input from user
    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
     // Do your map thing
    }
    @Override
    public void run(Context context) throws IOException, InterruptedException {
        setup(context);
        int count = 0 ;
        while (context.nextKeyValue()) {
            if(count++<numberOfRecordsToProcess){ // check if enough records has been processed already
                map(context.getCurrentKey(), context.getCurrentValue(), context);
            }else{
                break;
            }
        }
    }
    cleanup(context);
}

如何在hadoop/map reduce中创建固定行数的输出文件?,您可以使用此链接中的信息运行N行作为映射器输入,并且只运行main类中的一个映射器

setNumMapTasks(int) 

最新更新