Hadoop字数的意外输出

时间:2012-03-14 05:26:24

标签: java hadoop word-count

我修改了下面的代码,输出至少出现十次的单词。但它不起作用 - 输出文件根本不会改变。我需要做些什么来使其发挥作用?

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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

代码看起来有效。 为了能够帮助您,我们至少需要您用来运行它的命令行。如果你提供一个像这样的文件

,你可以发布实际的输出也会有所帮助
one
two two
three three three

等到20岁