强制映射器停止执行

时间:2014-03-20 19:00:40

标签: hadoop mapreduce

我有一个文件,我分成了分割,每个分割将被分配给一个映射器。 我想提出类似“预算时间”的东西,所以当映射器超过这个时间时,它会停止执行当前任务以及它负责的整个任务。我怎么能在Hadoop MapReduce中这样做。换句话说,如何在超出预算时间后强制映射器停止执行?

1 个答案:

答案 0 :(得分:0)

因此,如果控制预算时间'掌握在映射器本身之后,您可以覆盖run()类的org.apache.hadoop.mapreduce.Mapper,并且只执行map()直到预算时间'为止。

你可以这样:

public static class MapJob extends Mapper<LongWritable, Text, Text, Text> {

    private Text outputKey = new Text();
    private Text outputValue = new Text();
    private Date  startTime;
    private int budgetTimeInMilliSeconds;

    // in setup method set the startTime with surrent datetime
    @override
    public void setup(Context context){
        startTime = new Date();
        // also get the budgetTimeInMilliSeconds from configs
    }

    @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);
        while (context.nextKeyValue()) {
        Date currentTime = new Date();
            if(currentTime.getTime()-startTime.getTime() > budgetTimeInMilliSeconds) { 
                map(context.getCurrentKey(), context.getCurrentValue(), context);
            }else{
                break; // stop further execution of mapper
            }
        }
    }

    cleanup(context);
}
相关问题