九月十一日

3. 使用 MapReduce 实现词频统计

概述

MapReduce 是 Hadoop 用于处理大规模数据的核心编程模型。本文将通过 MapReduce 代码实现简单的词频统计任务。

内容

MapReduce 工作原理:Mapper 和 Reducer

Hadoop 项目结构

MapReduce 程序代码

代码示例

public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { 
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<Text, IntWritable, Text, IntWritable> { 
      public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { 
        int sum 0; for (IntWritable val : values) { sum += val.get();
       } context.write(key, new IntWritable(sum)); } } }
posted @   yblll  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-09-29 9.29日
2023-09-29 9.28日
2023-09-29 9.27日
2023-09-29 9.26日
2023-09-29 9.25日
点击右上角即可分享
微信分享提示