如何从设置方法停止映射任务?

时间:2014-07-03 13:42:28

标签: java hadoop mapreduce

我在作业类中有一些map类,我有时需要中断当前任务的执行(Hadoop Map-Reduce框架为作业的InputFormat生成的每个InputSplit生成一个map任务):

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

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        super.setup(context);
        // here I want to check some predicate, and may be break execution of task
        // http://hadoop.apache.org/docs/r2.3.0/api/org/apache/hadoop/mapreduce/Mapper.html
    }

    // continue....

2 个答案:

答案 0 :(得分:2)

您可以通过覆盖run()方法轻松破解它。

在普通代码中,这是这样实现的:

setup(context);
try {
  while (context.nextKeyValue()) 
    map(context.getCurrentKey(), context.getCurrentValue(), context);

} finally {
  cleanup(context);
}

你可以做的是围绕这个进行设置:

@Override
public void run(Mapper<LongWritable, Text, Text, Text>.Context context)
        throws IOException, InterruptedException {

   if(Predicate.runMapper(context)) {
      super.run(context); // do the usual setup/map/cleanup cycle
   }
}

这样,如果您的谓词告诉它,任务将直接进入完成状态。这还有一些开销,但比更改输入格式更容易。

答案 1 :(得分:0)

您无法在安装方法中中断执行。

但是,如果您在某些拆分中不执行映射器的逻辑是基于拆分号。那么你可以使用自定义的InputFormat和记录阅读器来跳过某些记录/输入分割。