式微胡不归 2018-11-07 13:10 采纳率: 66.7%
浏览 530
已采纳

hadoop第一个程序WordCount出现的问题

我用mapreduce做单词的统计
数据:

图片说明

map程序

public class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        Text word = new Text();
        LongWritable one = new LongWritable(1);
        //将Text转为String
        String line = value.toString();
        //分词
        String[] wordArr = line.split("\\s+");

        word.set(wordArr[0]);
        //将词的次数放入context
        context.write(word ,one);
    }
}

reduce程序:

public class MyReducer extends Reducer<Text,LongWritable,Text,LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        Long sum = 0L;
        for(LongWritable value:values){
            sum += value.get();
        }
        context.write(key,new LongWritable(sum));
    }
}

结果:
图片说明

我只想统计第一列的词汇
想要的结果是
word 1
hello 1
hahha 1
ahaha 1

到底哪里出错了
  • 写回答

1条回答

  • threenewbee 2018-11-07 13:52
    关注

    sum += value.get();
    ->
    sum = 1;
    break;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!