java - Hadoop WordCount 按单词出现次数排序

标签 java sorting hadoop mapreduce bigdata

我需要运行 WordCount这将给我所有的单词和它们的出现,但按出现次数而不是字母排序

我知道我需要为此创建两个作业并一个接一个地运行 我使用了 Sorted word count using Hadoop MapReduce 中的映射器和缩减器

package org.myorg;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapreduce.Job;

public class WordCount {

    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

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

    public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {

        public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
            int sum = 0;
            while (values.hasNext()) {
                sum += values.next().get();
            }
            output.collect(key, new IntWritable(sum));
        }
    }

    class Map1 extends MapReduceBase implements Mapper<Object, Text, IntWritable, Text> {

        public void map(Object key, Text value, OutputCollector<IntWritable, Text> collector, Reporter arg3) throws IOException {
            String line = value.toString();
            StringTokenizer stringTokenizer = new StringTokenizer(line);
            {
                int number = 999;
                String word = "empty";

                if (stringTokenizer.hasMoreTokens()) {
                    String str0 = stringTokenizer.nextToken();
                    word = str0.trim();
                }

                if (stringTokenizer.hasMoreElements()) {
                    String str1 = stringTokenizer.nextToken();
                    number = Integer.parseInt(str1.trim());
                }
                collector.collect(new IntWritable(number), new Text(word));
            }

        }

    }

    class Reduce1 extends MapReduceBase implements Reducer<IntWritable, Text, IntWritable, Text> {

        public void reduce(IntWritable key, Iterator<Text> values, OutputCollector<IntWritable, Text> arg2, Reporter arg3) throws IOException {
            while ((values.hasNext())) {
                arg2.collect(key, values.next());
            }
        }

    }

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

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

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

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

        FileInputFormat.setInputPaths(conf, new Path(args[0]));
        FileOutputFormat.setOutputPath(conf, new Path("/tmp/temp"));

    //JobClient.runJob(conf);
        //------------------------------------------------------------------
        JobConf conf2 = new JobConf(WordCount.class);
        conf2.setJobName("WordCount1");

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

        conf2.setMapperClass(Map1.class);
        conf2.setCombinerClass(Reduce1.class);
        conf2.setReducerClass(Reduce1.class);

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

        FileInputFormat.setInputPaths(conf2, new Path("/tmp/temp/part-00000"));
        FileOutputFormat.setOutputPath(conf2, new Path(args[1]));

        Job job1 = new Job(conf);
        Job job2 = new Job(conf2);

        job1.submit();
        if (job1.waitForCompletion(true)) {
            job2.submit();
    job1.waitForCompletion(true);
        }

    }
}

它不工作,我应该在这里更改什么,或者为什么它不工作???

最佳答案

如果程序运行到:

    INFO input.FileInputFormat: Total input paths to process : 1

那么问题出在你的最后一行:

    job2.submit();

作业已提交但未排队等待处理。试试这个:

    job1.submit();
    if (job1.waitForCompletion(true)) {
        job2.submit();
        job2.waitForCompletion(true);
    }

处理您的分拣机 MR 作业。我已经使用 MR 的新 API 尝试了您的代码,并且流程有效。

只需添加最后一行。

关于java - Hadoop WordCount 按单词出现次数排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22298432/

相关文章:

java - JJWT 依赖混淆

Java Cipher 表示 key 长度无效

Spring 数据流和 yarn : How to set properties properly?

scala - 使用嵌套字段更新数据框 - Spark

ios - 将 4 个不同数组的内容按相同顺序排序

scala - Spark BigQuery 连接器 : Writing ARRAY type causes exception: ""Invalid value for: ARRAY is not a valid value""

Java 9 Cleaner 正确用法

java - 当 StartNode 和 EndNode 是同一类型的对象时,Neo4j OGM RelationshipEntity

python - 按另一个字典排序字典

java - 为双向链表的实现编写一个 add 方法,该链表在我向其中添加对象时进行排序