Mapreduce字数Hadoop最高频率字

时间:2015-03-06 07:57:41

标签: java hadoop mapreduce word-count

因此,从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);
  }
}

3 个答案:

答案 0 :(得分:2)

一种可能的方法是将减速器的数量设置为&#34; 1&#34;。然后让减速器记住频率最高的字,并在清理时将其写入输出。像这样:

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));
    }
}

答案 1 :(得分:0)

您无法一步完成,每个键都会独立执行减少阶段(无法同步)。 解决方案是运行新的MapReduce作业,该作业将原始WordCount作业的输出聚合在一个键中,然后选择最大值。 GL!

答案 2 :(得分:0)

如果强制运行MapReduce只有一个Reduce任务,则在代码中实现搜索循环中所有键的主频率。

在此结束时,循环的输出包含具有主频率的键。您可以发送到最终输出的这一对(context.write()句子应该在结尾处执行一次)。