因此来自Hadoop教程网站(http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Source_Code)关于如何使用map reduce方法实现单词计数,我了解它是如何工作的,并且输出将是具有该频率的所有单词。
我想做的是只让输出是我所拥有的输入文件中频率最高的单词。
Example:
Jim
Jim
Jim
Jim
Tom
Dane
我希望输出只是
Jim 4
当前的单词输出计数是每个单词及其频率。有没有人编辑过单词计数,使其只打印频率最高的单词及其频率?
有人对如何做到这一点有什么建议吗?
我该如何编写另一个MapReducer,从WordCount的输出中找到频率最高的单词?
或者还有别的办法吗?
任何帮助都将不胜感激。
谢谢!
WordCount.jave:
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);
}
}
一种可能的方法是将减速器的数量设置为"1"。然后让一个reducer记住频率最高的单词,并在cleanup中将其写入输出。像这样:
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private Text tmpWord = new Text("");
private int tmpFrequency = 0;
@Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
if(sum > tmpFrequency) {
tmpFrequency = sum;
tmpWord = key;
}
}
@Override
public void cleanup(Context context) {
// write the word with the highest frequency
context.write(tmpWord, new IntWritable(tmpFrequency));
}
}
您不可能一步到位,reduce阶段是为每个键独立执行的(不可能同步)。解决方案是运行新的MapReduce作业,将原始WordCount作业的输出聚合在一个键中,只需选择最大GL!
如果您只使用一个Reduce任务强制运行MapReduce,那么在代码中,您将实现对循环中所有键的主频率的搜索。
最后,循环的输出包含具有主频率的键。这对可以发送到最终输出(context.write()
语句应该在最后执行一次)。