首页 > 解决方案 > 将两个 MapReduce 作业的结果连接在一起

问题描述

我正在尝试加入我从两个 MapReduce 作业中获得的结果。第一份工作返回 5 篇最有影响力的论文。下面是第一个 reducer 的代码。

import sys
import operator

current_word = None
current_count = 0
word = None
topFive = {}
# input comes from stdin
for line in sys.stdin:
    line = line.strip()

    # parse the input we got from mapper.py
    word, check = line.split('\t')
    if check != None:
        count = 1

    if current_word == word:
        current_count += count
    else:
        if current_word:
            topFive.update({current_word: current_count})
            #print(current_word, current_count)
        current_count = count
        current_word = word
if current_word == word:

    print(current_word, current_count)

t = sorted(topFive.iteritems(), key=lambda x:-x[1])[:6]
print("Top five most cited papers")
count = 1
for x in t:
    if x[0] != 'nan' and count <= 5:
        print("{0}: {1}".format(*x))
        count = count + 1

第二个工作找到5个最有影响力的作者,代码和上面的代码差不多。我想把这两个工作的结果加入他们,这样我就可以为每个作者确定他们最有影响力的 3 篇论文的平均引用次数。我不知道该怎么做,看来我需要以某种方式加入结果?

标签: pythonhadoopmapreducecloudera

解决方案


到目前为止,您将得到两个输出目录,一个用于作者,一个用于论文。

现在您想对这两个文件执行 JOIN 操作(如 DBs 术语)。为此,MapReduce 方法是创建第三个作业,对两个输出文件执行此操作。

对 Hadoop 中的 JOIN 操作进行了深入研究。一种方法是 reducer-side join 模式。该模式包括在映射器中创建两个子键的复合键(一个原始键 + 一个布尔键,指定是表 0 还是表 1)。

在使用 reducer 之前,您需要创建一个分隔这些复合键的分区器。reducer 只会从每个表中获取所有相同的键。

如果您需要进一步澄清,请告诉我,我写得很快。


推荐阅读