【问题标题】:How to re-run whole map/reduce in hadoop before job completion?如何在作业完成之前在 hadoop 中重新运行整个地图/减少?
【发布时间】:2011-07-08 10:08:26
【问题描述】:

我使用 Java 使用 Hadoop Map/Reduce

假设,我已经完成了整个 map/reduce 工作。有什么办法可以只重复整个地图/减少部分,而不结束工作。我的意思是,我不想使用任何不同作业的链接,而只想重复 map/reduce 部分。

谢谢!

【问题讨论】:

  • 正如您已经说过的,它是一个“MapReduce 作业”。因此,没有作业(在 hadoop 中)就没有 MapReduce,没有 MapReduce 就没有作业。你有什么问题?
  • 感谢您的回复,但我的意思是可以从 main() 函数一遍又一遍地运行 map() 和 reduce() 函数吗?我不想退出 main() 函数...
  • 所以你想从代码中调用map和reduce,或者你只是想等待工作完成并让main方法阻塞到那时?
  • 抱歉回复晚了,我生病了。是的,我希望工作等到第一个 map/reduce 过程完成。然后该作业应该再次调用 map/reduce 过程。

标签: java hadoop mapreduce chain


【解决方案1】:

所以我更熟悉 hadoop 流 API,但方法应该转化为原生 API。

据我了解,您尝试做的是对输入数据运行相同 map() 和 reduce() 操作的多次迭代。

假设您的初始 map() 输入数据来自文件 input.txt,输出文件是 output + {iteration}.txt(其中迭代是循环计数,迭代 =[0,迭代次数))。 在 map()/reduce() 的第二次调用中,您的输入文件是 output+{iteration} 并且输出文件将成为 output+{iteration +1}.txt。

如果不清楚,请告诉我,我可以想出一个快速示例并在此处发布链接。

EDIT* 所以对于 Java,我修改了 hadoop wordcount 示例以多次运行

package com.rorlig;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCountJob {
  public static class TokenizerMapper 
     extends Mapper<Object, Text, Text, IntWritable>{

 private final static IntWritable one = new IntWritable(1);
 private Text word = new Text();

 public void map(Object key, Text value, Context context
                ) throws IOException, InterruptedException {
  StringTokenizer itr = new StringTokenizer(value.toString());
  while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    context.write(word, one);
   }
 }
}

public static class IntSumReducer 
   extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values, 
                   Context context
                   ) throws IOException, InterruptedException {
  int sum = 0;
  for (IntWritable val : values) {
    sum += val.get();
  }
  result.set(sum);
  context.write(key, result);
  }
}

public static void main(String[] args) throws Exception {
 Configuration conf = new Configuration();

if (args.length != 3) {
  System.err.println("Usage: wordcount <in> <out> <iterations>");
  System.exit(2);
}
int iterations = new Integer(args[2]);
Path inPath = new Path(args[0]);
Path outPath =  null;
for (int i = 0; i<iterations; ++i){
    outPath = new Path(args[1]+i);
    Job job = new Job(conf, "word count");
    job.setJarByClass(WordCountJob.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, inPath);
    FileOutputFormat.setOutputPath(job, outPath);
    job.waitForCompletion(true);
    inPath = outPath;
   }
 }
}

希望对你有帮助

【讨论】:

  • 抱歉回复晚了,我生病了。感谢您的帮助。是的,就是这样。我要做的是从同一个工作中多次调用 map() 和 reduce()。
  • 我不能只重复reducers吗? 1 个 Mapper 和 n 个 Reducer 工作可能吗?
猜你喜欢
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多