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

Java 如何从设置方法停止映射任务?,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,我在作业类中有一些映射类,有时需要中断当前任务的执行(Hadoop map Reduce框架为作业的InputFormat生成的每个InputSplit生成一个映射任务): 公共静态类TestJobMapper 扩展映射器{ @凌驾 受保护的无效设置(上下文上下文)引发IOException、InterruptedException{ 超级设置(上下文); //这里我想检查一些谓词,可能会中断任务的执行 // http://hadoop.apache.org/docs/r2.3.0/api/or

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

公共静态类TestJobMapper
扩展映射器{
@凌驾
受保护的无效设置(上下文上下文)引发IOException、InterruptedException{
超级设置(上下文);
//这里我想检查一些谓词,可能会中断任务的执行
// http://hadoop.apache.org/docs/r2.3.0/api/org/apache/hadoop/mapreduce/Mapper.html
}
//继续。。。。

您不能在设置方法中断执行


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

您可以通过重写
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
   }
}
@覆盖
公共void运行(Mapper.Context上下文)
抛出IOException、InterruptedException{
if(谓词.runMapper(上下文)){
super.run(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
   }
}