为了账号安全,请及时绑定邮箱和手机立即绑定

无法从 Object 转换为 IntWritable

无法从 Object 转换为 IntWritable

弑天下 2021-12-18 09:54:00
当我尝试在 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);        }    }错误发生在这里:
查看完整描述

1 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

您需要在实现时指定泛型类型 Reducer


public static class IntSumReducer

       extends Reducer<Text,IntWritable,Text,IntWritable> {

    private IntWritable result = new IntWritable();


    public void reduce(Text key, Iterable<IntWritable> values,

                       Context context

                       ) throws IOException, InterruptedException {

      int sum = 0;

      for (IntWritable val : values) {

        sum += val.get();

      }

      result.set(sum);

      context.write(key, result);

    }

  }


查看完整回答
反对 回复 2021-12-18
  • 1 回答
  • 0 关注
  • 180 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信