Hadoop中Map-Reduce处理逻辑理解(转)

本文介绍如何利用MapReduce模型与算法处理天气数据,通过解析数据格式,实现统计每年最高温度的功能。
MapReduce is a programming model and an associated implementation for processing and generating large data sets. Users specify a map function that processes a key/value pair to generate a set of intermediate key/value pairs, and a reduce function that merges all intermediate values associated with the same intermediate key. Many real world tasks are expressible in this model, as shown in the paper. 

转自: http://www.360doc.com/content/10/1116/16/2703996_69876858.shtml  

1、Map-Reduce的逻辑过程 
假设我们需要处理一批有关天气的数据,其格式如下: 
  • 按照ASCII码存储,每行一条记录
  • 每一行字符从0开始计数,第15个到第18个字符为年
  • 第25个到第29个字符为温度,其中第25位是符号+/-

006701199099999 1950 051507 +0000
004301199099999 1950 051512 +0022
004301199099999 1950 051518 -0011
004301265099999 1949 032412 +0111
004301265099999 1949 032418 +0078
006701199099999 1937 051507 +0001
004301199099999 1937 051512 -0002
004301199099999 1945 051518 +0001
004301265099999 1945 032412 +0002
004301265099999 1945 032418 +0078

现在需要统计出每年的最高温度。 
Map-Reduce主要包括两个步骤:Map和Reduce 
每一步都有key-value对作为输入和输出: 
  • map阶段的key-value对的格式是由输入的格式所决定的,如果是默认的TextInputFormat,则每行作为一个记录进程处理,其中key为此行的开头相对于文件的起始位置,value就是此行的字符文本
  • map阶段的输出的key-value对的格式必须同reduce阶段的输入key-value对的格式相对应

对于上面的例子,在map过程,输入的key-value对如下: 
(0 ,006701199099999 1950 051507 +0000 +) 
(1 ,004301199099999 1950 051512 +0022 +) 
(2 ,004301199099999 1950 051518 -0011 +) 
(3 ,004301265099999 1949 032412 +0111 +) 
(4 ,004301265099999 1949 032418 +0078 +) 
(5 ,006701199099999 1937 051507 +0001 +) 
(6 ,004301199099999 1937 051512 -0002 +) 
(7 ,004301199099999 1945 051518 +0001 +) 
(8 ,004301265099999 1945 032412 +0002 +) 
(9 ,004301265099999 1945 032418 +0078 +) 

在map过程中,通过对每一行字符串的解析,得到年-温度的key-value对作为输出: 
(1950, 0) 
(1950, 22) 
(1950, -11) 
(1949, 111) 
(1949, 78) 
(1937, 1) 
(1937, -2) 
(1945, 1) 
(1945, 2) 
(1945, 78) 

在reduce过程,将map过程中的输出,按照相同的key将value放到同一个列表中作为reduce的输入 
(1950, [0, 22, –11]) 
(1949, [111, 78]) 
(1937, [1, -2]) 
(1945, [1, 2, 78]) 

在reduce过程中,在列表中选择出最大的温度,将年-最大温度的key-value作为输出: 
(1950, 22) 
(1949, 111) 
(1937, 1) 
(1945, 78) 

其逻辑过程可用如下图表示: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值