• 【Hadoop】MapReduce 编程案例-WordCount


    欢迎点击此处关注公众号。
    一个完整的MapReduce程序在分布式运行时有三类实例进程:

    (1)MrAppMaster:负责整个程序的过程调度及状态协调。

    (2)MapTask:负责 Map 阶段的整个数据处理流程。

    (3)ReduceTask:负责 Reduce 阶段的整个数据处理流程。

    1.Mapper 阶段

    (1)用户自定义的 Mapper 要继承自己的父类;

    (2)Mapper 的输入数据是 KV 对的形式(KV 的类型可自定义);

    (3)Mapper 中的业务逻辑写在 map() 方法中;

    (4)Mapper 的输出数据是 KV 对的形式(KV 的类型可自定义)

    (5)map() 方法(MapTask 进程)对每一个 调用一次。

    2.Reducer 阶段

    (1)用户自定义的 Reducer 要继承自己的父类;

    (2)Reducer 的输入数据类型对应 Mapper 的输出数据类型,也是 KV;

    (3)Reducer 的业务逻辑写在 reduce() 方法中;

    (4)ReduceTask 进程对每一组相同 k 的 组调用一次 reduce() 方法。

    3.WordCount 案例

    输入:单词

    输出:,即 (单词,数量)。

    (1)编写Mapper类

    public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
    	
    	Text k = new Text();
    	IntWritable v = new IntWritable(1);
    	
    	@Override
    	protected void map(LongWritable key, Text value, Context context)	throws IOException, InterruptedException {
    		
    		// 1 获取一行
    		String line = value.toString();
    		
    		// 2 切割
    		String[] words = line.split(" ");
    		
    		// 3 输出
    		for (String word : words) {
    			k.set(word);
    			context.write(k, v);
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    (2)编写 Reducer 类

    public class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
    
        int sum;
        IntWritable v = new IntWritable();
    
    	@Override
    	protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
    		
    		// 1 累加求和
    		sum = 0;
    		for (IntWritable count : values) {
    			sum += count.get();
    		}
    		
    		// 2 输出
    		v.set(sum);
    		context.write(key,v);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    关于找暑期实习后的一些反思
    MySQL——增删改查
    08 | Harbor 不可用排查方法
    多个方法多个出路!Microsoft Excel中合并单元格的8种方法
    量化研究丨波动与盈利关系研究系列(一)
    初学SpringMVC之 Ajax 篇
    Master公式-递归时间复杂度度量
    变长子网划分问题的二叉树解法
    Qt 4.8.6 的下载与安装
    Android轮播图控件com.github.pinguo-zhouwei:MZBannerView:v2.0.2
  • 原文地址:https://blog.csdn.net/weixin_45545090/article/details/126600084