用于构建mapreduce作业原型的python库
mockr的Python项目详细描述
模拟机
mockr是一个python库,用于在教育环境中编写mapreduce作业。它的用途是 概念教学工具。
mockr提供了一个接口,用于在本地定义和运行mapreduce。只需定义map和reduce函数, 输入数据并调用run函数。一切都是按顺序和本地运行的。
安装
pip install mockr
文档
这里有完整的文档https://mockr.readthedocs.io/
流作业
streamingjob类,该类期望输入为字符的字节流。数据块由 换行符(“\n”)字符。每一行都被发送到一个单独的地图工作者。
原生python序列作业
pythonjob类期望输入为collections.abc.sequence类型对象,例如python list。python作业提供两个 执行方法:
- 序列被分成块,每个块被发送到单独的映射工作器
- 列表中的每个项目都单独发送给一个专用的地图工作者
熊猫工作
pandasjob类希望输入为pandas数据帧。数据帧的行平均分为块和 每个块都被发送到一个单独的map worker
示例用法
import re
from mockr import run_stream_job
WORD_RE = re.compile(r"[\w']+")
def map_fn(chunk):
# yield each word in the line
for word in WORD_RE.findall(chunk):
yield (word.lower(), 1)
def reduce_fn(key, values):
yield (key, sum(values))
input_str = "Hello!\nThis is a sample string.\nIt is very simple.\nGoodbye!"
results = run_stream_job(input_str, map_fn, reduce_fn)
print(results)
输出:
[('hello', 1), ('this', 1), ('is', 2), ('a', 1), ('sample', 1), ('string', 1), ('it', 1), ('very', 1), ('simple', 1), ('goodbye', 1)]