无法从 Object 转换为 IntWritable

当我尝试在 Eclipse(用于 Hadoop)中运行 wordcount 示例时遇到问题。在包含所有 hadoop 库后,我仍然收到错误消息。如果有人有任何想法,那将是完美的。谢谢


这是我的 Wordcount 类:


import java.io.IOException;

import java.util.StringTokenizer;


import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapred.WordCount;

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;


public class Wordcount {


    public static class TokenizerMapper extends Mapper {


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

        private Text word = new Text();


        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

            StringTokenizer itr = new StringTokenizer(value.toString());

            while (itr.hasMoreTokens()) {

                word.set(itr.nextToken());

                context.write(word, one);

            }

        }

    }


    public static class IntSumReducer extends Reducer {

        private IntWritable result = new IntWritable();


        public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {

            int sum = 0;

            for (IntWritable val : values) {

                sum += val.get();

            }

            result.set(sum);

            context.write(key, result);

        }

    }

错误发生在这里:

http://img.mukewang.com/61bd3f53000138d709850182.jpg

弑天下
浏览 181回答 1
1回答

PIPIONE

您需要在实现时指定泛型类型 Reducerpublic static class IntSumReducer&nbsp; &nbsp; &nbsp; &nbsp;extends Reducer<Text,IntWritable,Text,IntWritable> {&nbsp; &nbsp; private IntWritable result = new IntWritable();&nbsp; &nbsp; public void reduce(Text key, Iterable<IntWritable> values,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Context context&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) throws IOException, InterruptedException {&nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; for (IntWritable val : values) {&nbsp; &nbsp; &nbsp; &nbsp; sum += val.get();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; result.set(sum);&nbsp; &nbsp; &nbsp; context.write(key, result);&nbsp; &nbsp; }&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java