【发布时间】:2013-12-14 08:21:09
【问题描述】:
我用 Python 编写了一个 Mapper 和 Reducer,并使用 Hadoop Streaming 在 Amazon 的 Elastic MapReduce(EMR) 上成功执行了它。
最终结果文件夹包含三个不同文件 part-00000、part-00001 和 part-00002 中的输出。但我需要将输出作为一个文件。有什么办法可以做到吗?
这是我的 Mapper 代码:
#!/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)
我需要将其输出为一个文件。
【问题讨论】:
-
你不能直接打开这三个文件并将它们连接成一个输出文件吗?
-
这就是我一直在做的。但如果我能在 Reduce 阶段之后得到一个输出文件,我会很高兴。
-
你就不能这样做吗(Linux/UNIX):
cat part-00000 part-00001 part-00002 > output? -
谢谢詹姆斯。这是一种方式。但是我不能让 EMR 本身将它作为一个单独的部分文件吐出来?
标签: python hadoop mapreduce hadoop-streaming elastic-map-reduce