如何从安装方法停止Map任务?

z3yyvxxp  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(421)

我在job类中有一些map类,有时需要中断当前任务的执行(hadoop map reduce框架为job的inputformat生成的每个inputspilt生成一个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....
j8ag8udp

j8ag8udp1#

通过重写 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
   }
}

这样,如果 predicate 告诉任务,任务就直接进入完成状态。这仍然有一些开销,但它比更改输入格式更容易。

46scxncf

46scxncf2#

不能在setup方法处中断执行。
但是,如果不在某个拆分上执行Map器的逻辑是基于拆分编号的,则可以使用自定义的inputformat和record reader跳过某些记录/输入拆分。

相关问题