java - 如何按值或计数对单词计数程序进行排序?

标签 java hadoop mapreduce

如何按计数/值而不是键对字数输出进行排序。

在正常情况下,输出为

hi 2
hw 3 
wr 1 
r 3

但所需的输出是
wr 1
hi 2
hw 3
r 3

我的代码是:
public class sortingprog {
     public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, IntWritable, Text> {
         private final static IntWritable one = new IntWritable(1);
         private Text word = new Text();

         public void map(LongWritable key, Text value, OutputCollector<IntWritable, Text> output, Reporter reporter) throws IOException {
           String line = value.toString();
           StringTokenizer tokenizer = new StringTokenizer(line);
           while (tokenizer.hasMoreTokens()) {
             word.set(tokenizer.nextToken());
             output.collect(one,word);
           }
         }
       }


     public static class Reduce extends MapReduceBase implements Reducer<IntWritable,Text, IntWritable, Text> {
     public void reduce(Iterator<IntWritable> key, Text value, OutputCollector<IntWritable, Text> output, Reporter reporter) throws IOException {
            int sum=0;
           while (key.hasNext()) {
             sum+=key.next().get();
           }
           output.collect(new IntWritable(sum),value);

     }

    @Override
    public void reduce(IntWritable arg0, Iterator<Text> arg1,
            OutputCollector<IntWritable, Text> arg2, Reporter arg3)
            throws IOException {
        // TODO Auto-generated method stub

    }
     }

     public static class GroupComparator extends WritableComparator {
            protected GroupComparator() {
                super(IntWritable.class, true);
            }

            @SuppressWarnings("rawtypes")
            @Override
            public int compare(WritableComparable w1, WritableComparable w2) {
                IntWritable v1 = (IntWritable) w1;
                IntWritable v2 = (IntWritable) w2;          
                return -1 * v1.compareTo(v2);
            }
        }

       public static void main(String[] args) throws Exception {
         JobConf conf = new JobConf(sortingprog.class);
         conf.setJobName("wordcount");


         conf.setOutputKeyClass(IntWritable.class);
         conf.setOutputValueClass(Text.class);


         conf.setMapperClass(Map.class);
         conf.setReducerClass(Reduce.class);

         conf.setOutputValueGroupingComparator(GroupComparator.class);

         conf.setInputFormat(TextInputFormat.class);
         conf.setOutputFormat(TextOutputFormat.class);

         FileInputFormat.setInputPaths(conf, new Path(args[0]));
         FileOutputFormat.setOutputPath(conf, new Path(args[1]));

         JobClient.runJob(conf);
       }
}

最佳答案

您要查找的内容称为“第二排序”。在这里,您可以找到两个有关如何在MapReduce中实现短值的教程:

http://vangjee.wordpress.com/2012/03/20/secondary-sorting-aka-sorting-values-in-hadoops-mapreduce-programming-paradigm/

http://codingjunkie.net/secondary-sort/

关于java - 如何按值或计数对单词计数程序进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23266974/

相关文章:

java - 在JAVA中显示 "Correct"和 "Wrong"

c# - 为盲人提供无障碍应用

scala - 如何在不因org.apache.spark.sql.AnalysisException而失败的情况下插入覆盖Hive表:只能将数据写入到具有单个路径的关系中?

hadoop - 在 MapReduce 作业中增加 ZooKeeper 协商超时

apache - Hadoop DistributedCache缓存文件没有绝对路径?

java - CXF:如何动态提供SSL证书?

java - 使用 Robolectric,如何在运行测试类中的所有测试方法之前和之后初始化和完成内容

java - Hadoop FileAlreadyExistsException : Output directory hdfs://<namenode public dns>:9000/input already exists

hadoop - 在不使用配置单元的情况下在HDFS中进行数据清理

hadoop - 在Map/Reduce中,只能减少重新启动吗?