0

我使用 Java 使用 Hadoop Map/Reduce

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

谢谢!

4

1 回答 1

7

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

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

假设您的初始 map() 输入数据来自文件 input.txt ,输出文件是 output + {iteration}.txt (其中迭代是循环计数,迭代 =[0,迭代次数))。在 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;
   }
 }
}

希望这可以帮助

于 2011-04-18T19:52:46.857 回答