1

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

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

4 回答 4

1

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

于 2012-03-14T12:17:47.273 回答
0

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

于 2012-03-14T14:52:57.357 回答
0

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

one
two two
three three three

等到20

于 2012-03-14T20:26:30.333 回答
0

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

于 2012-03-14T20:25:44.327 回答