java - 如何在作业完成之前在 hadoop 中重新运行整个 map/reduce?

标签 java hadoop mapreduce chain

我使用 Hadoop Map/Reduce 使用 Java

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

谢谢!

最佳答案

所以我更熟悉 hadoop 流式 API,但方法应该转换为 native API。

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

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

如果不清楚请告诉我,我可以想出一个简单的例子并在此处发布链接。

编辑* 因此,对于 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;
   }
 }
}

希望对你有帮助

关于java - 如何在作业完成之前在 hadoop 中重新运行整个 map/reduce?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5702018/

相关文章:

hadoop - 如何在mapreducer和hbase之间拆分内存

java - 是否可以将 CORS header 添加到 Tomcat 容器中来自 j_security_check 的响应

hadoop - 如何在hadoop配置中为datanode分配内存

在 2 节点集群中使用压缩时 Hadoop 映射任务失败。但是当作为单个节点运行时,两个节点都工作正常

hadoop - Hadoop机架拓扑

linux - 看不到 hdfs,Hadoop shell 命令 hadoop fs -ls 给出错误无法访问

java - mapreduce 类中的奇怪错误

错误跟踪日志记录的 Java 实现

java - 特定于枚举常量的类主体是静态的还是非静态的?

java - ProgID和文件扩展名关系