返回介绍

The Hadoop MapReduce workflow

发布于 2025-02-25 23:44:05 字数 1415 浏览 0 评论 0 收藏 0

A Hadoop job consists of the input file(s) on HDFS, \(m\) map tasks and \(n\) reduce tasks, and the output is \(n\) files. The stages of one map-reduce iteration are:

  • mapper (written by programmer)
  • combiner (written by programmer)
  • sort and shuffle (done by Hdaoop framework)
  • reduce (written by programmer)

At each such iteration, there is input read in from HDFS and given to the mapper, and output written out to HDFS by the reducer. Optimizing the MapReduce pipeline often consists of minimizing the I/O tranfers.

Illustrating ideas behind MapReduce with a toy example of counting the number of each character in a string

Input

s = 'aabaabcdeda'

Map to create a key-value pair

xs = map(lambda x: [x, 1], s)
xs
[['a', 1],
 ['a', 1],
 ['b', 1],
 ['a', 1],
 ['a', 1],
 ['b', 1],
 ['c', 1],
 ['d', 1],
 ['e', 1],
 ['d', 1],
 ['a', 1]]

Sort and shuffle (aggregate and transfer data)

xs = sorted(xs)
ys = []
seen = []
for x in xs:
    if x[0] not in seen:
        seen.append(x[0])
        ys.append([x[0], [x[1]]])
    else:
        ys[-1][1].append(x[1])
ys
[['a', [1, 1, 1, 1, 1]], ['b', [1, 1]], ['c', [1]], ['d', [1, 1]], ['e', [1]]]

Reduce

for y in ys:
    print y[0], reduce(add, y[1])
a 5
b 2
c 1
d 2
e 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文