gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:22:28 26 4
gpt4 key购买 nike

我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com