现在MapReduce貌似已经开始变成标配了,当然还有一种说法就是已经烂大街了。今天要提到这个是一个纯python编写的MapReduce引擎,整个引擎只有一个python文件。下面学步园小编来讲解下MapReduce引擎怎么实现?
MapReduce引擎怎么实现
使用例子见下,只需要import一个名即可。
#!/usr/bin/env python
import mincemeat
data = ["Humpty Dumpty sat on a wall",
"Humpty Dumpty had a great fall",
"All the King's horses and all the King's men",
"Couldn't put Humpty together again",
]
def mapfn(k, v):
for w in v.split():
yield w, 1
def reducefn(k, vs):
MapReduce引擎怎么实现
result = 0
for v in vs:
result += v
return result
s = mincemeat.Server()
# The data source can be any dictionary-like object
s.datasource = dict(enumerate(data))
s.mapfn = mapfn
s.reducefn = reducefn
results = s.run_server(password="changeme")
print results
然后将mincemeat.py 和example.py 放在同一个目录下,执行example.py
python example.py
这时程序会挂起一个daemon
然后另开一终端运行:
python mincemeat.py -p changeme localhost
就会看到刚才的daemon打印出了MapReduce结果并退出了。
{‘a’: 2, ‘on’: 1, ‘great’: 1, ‘Humpty’: 3, ‘again’: 1, ‘wall’: 1, ‘Dumpty’: 2, ‘men’: 1, ‘had’: 1, ‘all’: 1, ‘together’: 1, “King’s”: 2, ‘horses’: 1, ‘All’: 1, “Couldn’t”: 1, ‘fall’: 1, ‘and’: 1, ‘the’: 2, ‘put’: 1, ’sat’: 1}
这是最简单的例子,daemon没有数据输入输出,也没有执行多次任务。相信你能根据它写出更复杂的东西来。
以上就是关于“MapReduce引擎怎么实现”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!