我用 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/