0
我写了一个简单的地图,并减少在Python中的程序来计算每个句子的数字,然后将相同的数字组合在一起。即假设句子1有10个单词,句子2有17个单词,句子3有10个单词。最终的结果将是:亚马逊MapReduce与我自己的reducer流式传输
10 \t 2
17 \t 1
映射器功能是:
import sys
import re
pattern = re.compile("[a-zA-Z][a-zA-Z0-9]*")
for line in sys.stdin:
word = str(len(line.split())) # calculate how many words for each line
count = str(1)
print "%s\t%s" % (word, count)
的减速功能是:
import sys
current_word = None
current_count = 0
word = None
for line in sys.stdin:
line = line.strip()
word, count = line.split('\t')
try:
count = int(count)
word = int(word)
except ValueError:
continue
if current_word == word:
current_count += count
else:
if current_word:
print "%s\t%s" % (current_word, current_count)
current_count = count
current_word = word
if current_word == word:
print "%s\t%s" %(current_word, current_count)
我在我的本地机器上测试了第200行的file: head -n 200句子.txt | python mapper.py |排序| python reducer.py 结果是正确的。然后我使用Amazon MapReduce流媒体服务,它在缩小步骤失败。于是我将打印机中的打印功能更改为:
print "LongValueSum" + word + "\t" + "1"
这适合于mapreduce流服务中的默认聚合。在这种情况下,我不需要reducer.py函数。我从大文件句子.txt得到最终结果。但我不知道为什么我的reducer.py函数失败。谢谢!
您可能想要查看mrjob:https://pythonhosted.org/mrjob/这是一种使用Python编写MapReduce作业的非常方便的方法。可以在本地开发一个小样本数据集,然后使用亚马逊的Elastic-Mapreduce对命令行进行轻微调整,然后将其扩大到更大的数据集。 – 2014-10-27 02:18:34