式微胡不归 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;

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵