分享

Hadoop计算单词个数有问题!请大家帮忙看下

xs.cctv@163.com 发表于 2014-3-15 23:00:29 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 4982
本帖最后由 xs.cctv@163.com 于 2014-3-16 09:49 编辑

package wordcount;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
* @author xiao
*/
public class WordCount {

        /**
         *
         * @param args
         * @throws IOException
         * @throws ClassNotFoundException
         * @throws InterruptedException
         */
        public static void main(String[] args) throws IOException,
                        ClassNotFoundException, InterruptedException {
                Job job = new Job();
                job.setJarByClass(WordCount.class);
                job.setJobName("word count demo");

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

                job.setMapperClass(Map.class);
                job.setCombinerClass(Reducer.class);
                job.setReducerClass(Reducer.class);

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

                System.exit(job.waitForCompletion(true) ? 0 : 1);
        }

        public static class Map extends
                        Mapper<LongWritable, Text, Text, IntWritable> {
                // A fixed value is 1
                private final static IntWritable one = new IntWritable(1);
                private Text word = new Text();

                @Override
                protected 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> {
                private static IntWritable result = new IntWritable();
               
                @Override
                protected void reduce(Text key, Iterable<IntWritable> values,
                                Context context) throws IOException, InterruptedException {
                        int sum = 0;
                        for (IntWritable value : values) {
                                sum = value.get();
                        }                        
                        result.set(sum);
                        context.write(key, result);
                }
        }
}


上面的是代码,计算出来的的单词
Hello        1
How        1
This        1
a        1
are        1
are        1
is        1
ok        1
test        1
wrod,        1
you        1
you        1
是这个样子,不知道哪里有错误,已经和网上的对比了没有发现问题。

没找到任何评论,期待你打破沉寂

xs.cctv@163.com 发表于 2014-3-16 09:49:40
问题已经解决,谢谢大家的帮助!

protected void map(LongWritable key, Text value, Context context)
                                throws IOException, InterruptedException {

protected void reduce(Text key, Iterable<IntWritable> values,
                                Context context) throws IOException, InterruptedException {


覆盖父类的方法不能写protected 应该写成public 就可以出现结果了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条