python - 合并 MapReduce 作业的输出文件

标签 python hadoop mapreduce hadoop-streaming elastic-map-reduce

我用 Python 编写了 Mapper 和 Reducer,并使用 Hadoop Streaming 在 Amazon 的 Elastic MapReduce (EMR) 上成功执行了它。

最终结果文件夹包含三个不同文件 part-00000、part-00001 和 part-00002 中的输出。但我需要将输出作为一个文件。有什么办法可以做到吗?

这是我的映射器代码:

#!/usr/bin/env python

import sys

for line in sys.stdin:
    line = line.strip()
    words = line.split()
    for word in words:
        print '%s\t%s' % (word, 1)

这是我的 Reducer 代码

#!/usr/bin/env python

from operator import itemgetter
import sys

current_word = None
current_count = 0
word = None
max_count=0

for line in sys.stdin:
    line = line.strip()
    word, count = line.split('\t', 1)

    try:
        count = int(count)
    except ValueError:
        continue

if current_word == word:
    current_count += count
else:
    if current_word:
        # write result to STDOUT
            if current_word[0] != '@':
                print '%s\t%d' % (current_word, current_count)
                if count > max_count:
                    max_count = count
    current_count = count
    current_word = word

if current_word == word:
    print '%s\t%s' % (current_word, current_count)

我需要将其输出为一个文件。

最佳答案

一个非常简单的方法(假设是 Linux/UNIX 系统):

$ cat part-00000 part-00001 part-00002 > output

关于python - 合并 MapReduce 作业的输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20581137/

相关文章:

java - Hadoop MultipleOutputs.addNamedOutput 抛出 "cannot find symbol"

javascript - 沙发基地在 node.js 中按日期减少

java - 在多个 Map-Reduce 作业之间传递数据库连接对象

python - 更改 python 中列表列表的代码

testing - Hadoop Map Reduce测试-自定义记录读取器

hadoop - 如何从 Ambari 2.3 集群中完全删除 Ranger Admin Server 和 Ranger KMS

hadoop - reduce任务输入不平衡如何处理?

python - 类型错误 : 'tuple' object is not callable in pygame

python - 使用安全删除的用户进行 DRF 和 token 身份验证?

python - 如何在 python 中创建 sha1 哈希