【问题标题】:Unexpected output from Hadoop word countHadoop 字数统计的意外输出
【发布时间】:2012-04-27 22:21:11
【问题描述】:

我修改了下面的代码以输出至少出现十次的单词。但它不起作用——输出文件根本没有改变。我必须做些什么才能让它发挥作用?

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

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;
// ...
public class WordCount extends Configured implements Tool {
// ...
public static class Map extends 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, Context context)
            throws IOException, InterruptedException {
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
            word.set(tokenizer.nextToken());
            context.write(word, one);
        }
    }
}

public static class Reduce extends
        Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterable<IntWritable> values,
            Context context) throws IOException, InterruptedException {

        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
                    // where I modified, but not working, the output file didnt change
        if(sum >= 10)
        {
            context.write(key, new IntWritable(sum));
        }
    }
}

public int run(String[] args) throws Exception {
    Job job = new Job(getConf());
    job.setJarByClass(WordCount.class);
    job.setJobName("wordcount");

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

    job.setMapperClass(Map.class);
    //job.setCombinerClass(Reduce.class);
    job.setReducerClass(Reduce.class);

    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

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

    boolean success = job.waitForCompletion(true);
    return success ? 0 : 1;
}

public static void main(String[] args) throws Exception {
    int ret = ToolRunner.run(new WordCount(), args);
    System.exit(ret);
}
}

【问题讨论】:

    标签: java hadoop word-count


    【解决方案1】:

    代码看起来完全有效。我可以怀疑您的数据集足够大,所以单词恰好出现超过 10 次? 请确保您确实在寻找新的结果..

    【讨论】:

      【解决方案2】:

      您可以看到默认的 Hadoop 计数器并了解发生了什么。

      【讨论】:

      • 具体来说,如果reduce输入组的数量等于reduce输出记录的数量,并且你已经确认你的所有输出的计数>=10,那么这支持@David留下的评论
      【解决方案3】:

      代码看起来有效。 为了能够帮助您,我们至少需要您用来运行它的命令行。如果您向它提供这样的文件,如果您可以发布实际输出也会有所帮助

      one
      two two
      three three three
      

      等到20岁

      【讨论】:

        【解决方案4】:

        代码绝对正确,也许您正在阅读修改代码之前生成的输出。或者你修改代码后没有更新之前使用的jar文件?

        【讨论】:

          猜你喜欢
          • 2021-09-12
          • 2013-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-31
          相关资源
          最近更新 更多