Python结合Shell/Hadoop实现MapReduce
2024-08-24 10:43:04
基本流程为:
cat data | map | sort | reduce
cat devProbe | ./mapper.py | sort| ./reducer.py
echo "foo foo quux labs foo bar quux" | ./mapper.py | sort -k1,1 | ./reducer.py
# -k, -key=POS1[,POS2] 键以pos1开始,以pos2结束
如不执行下述命令,可以再py文件前加上python调用
chmod +x mapper.py
chmod +x reducer.py
对于分布式环境下,可以使用以下命令:
hadoop jar /[YOUR_PATH]/hadoop/tools/lib/hadoop-streaming-2.6.0-cdh5.4.4.jar \
-file mapper.py -mapper mapper.py \
-file reducer.py -reducer reducer.py \
-input [IN_FILE] -output [OUT_DIR]
mapper.py
#!/usr/bin/python
# -*- coding: UTF-8 -*- __author__ = 'Manhua' import sys
for line in sys.stdin:
line = line.strip()
item = line.split('`')
print "%s\t%s" % (item[0]+'`'+item[1], 1)
reducer.py
#!/usr/bin/python
# -*- coding: UTF-8 -*- __author__ = 'Manhua' import sys current_word = None
current_count = 0
word = None for line in sys.stdin:
line = line.strip()
word, count = line.split('\t', 1)
try:
count = int(count)
except ValueError: #count如果不是数字的话,直接忽略掉
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 word == current_word: #不要忘记最后的输出
print "%s\t%s" % (current_word, current_count)
最新文章
- FineReport:任意时刻只允许在一个客户端登陆账号的插件
- [转载]T-SQL(Oracle)语句查询执行顺序
- 深入理解Oracle的并行操作-转载
- 关于js性能
- vs2010设置
- Javascript基础系列之(五)条件语句(if条件语句)
- jquery的show/hide性能测试
- vpn分配多ip的配置
- 月薪10K必备--C#下拉框联动
- talos项目记录
- 原生js的一些研究和总结(1)
- JSBridge的实现
- java动态代理实现与原理详细分析
- 华莱士的 第二个python程序之(格式化输出)
- Python装饰器执行顺序详解
- stm32f7699遇到的犯二问题
- Spring Boot 技术总结
- codeforces水题100道 第十八题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table (brute force)
- bootstrap-table 切换页码保留勾选的checkbox
- 在 IE 浏览器中,使用 bootstrap 使得页面滚动条浮动显示,自动隐藏,自动消失