gpt4 book ai didi

java - Hadoop MapReduce作业可实现最高频率

转载 作者:行者123 更新时间:2023-12-02 21:01:42 24 4
gpt4 key购买 nike

我正在尝试使用定义的here的基本单词计数。当IntSumReducer执行context.write时,是否有可能将该context.write传递给第二个reducer或输出类,该类将把IntSumReducer给出的最终列表减少/更改为单个最大频率?

我对Hadoop / MapReduce和Java中作业的概念还很陌生,因此我不确定我到底需要多少修改默认WordCount才能使其实现。我可以编写第二个Reducer函数并将其放置在同一作业中吗?我该怎么办?我如何发出信号,指示在IntSumReducer之后要运行另一个reducer?

基本字数:

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 WordCount {

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();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}`

最佳答案

您正在寻找的东西在hadoop中称为Combiner,它在将输出发送到最终的reducer类之前进行一些半还原。有关更多信息,请单击here

关于java - Hadoop MapReduce作业可实现最高频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43086657/

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