Hadoop中Map-Reduce处理逻辑理解

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 1950051507 +0000+
004301199099999 1950051512 +0022+
004301199099999 1950051518 -0011+
004301265099999 1949032412 +0111+
004301265099999 1949032418 +0078+
006701199099999 1937051507 +0001+
004301199099999 1937051512 -0002+
004301199099999 1945051518 +0001+
004301265099999 1945032412 +0002+
004301265099999 1945032418 +0078+

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

对于上面的例子,在map过程,输入的key-value对如下:
(0 ,006701199099999 1950051507 +0000+)
(1 ,004301199099999 1950051512 +0022+)
(2 ,004301199099999 1950051518 -0011+)
(3 ,004301265099999 1949032412 +0111+)
(4 ,004301265099999 1949032418 +0078+)
(5 ,006701199099999 1937051507 +0001+)
(6 ,004301199099999 1937051512 -0002+)
(7 ,004301199099999 1945051518 +0001+)
(8 ,004301265099999 1945032412 +0002+)
(9 ,004301265099999 1945032418 +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)

其逻辑过程可用如下图表示:
Hadoop中Map-Reduce处理逻辑理解

你可能感兴趣的:(mapreduce,hadoop)