基本流程为:

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)

最新文章

  1. FineReport:任意时刻只允许在一个客户端登陆账号的插件
  2. [转载]T-SQL(Oracle)语句查询执行顺序
  3. 深入理解Oracle的并行操作-转载
  4. 关于js性能
  5. vs2010设置
  6. Javascript基础系列之(五)条件语句(if条件语句)
  7. jquery的show/hide性能测试
  8. vpn分配多ip的配置
  9. 月薪10K必备--C#下拉框联动
  10. talos项目记录
  11. 原生js的一些研究和总结(1)
  12. JSBridge的实现
  13. java动态代理实现与原理详细分析
  14. 华莱士的 第二个python程序之(格式化输出)
  15. Python装饰器执行顺序详解
  16. stm32f7699遇到的犯二问题
  17. Spring Boot 技术总结
  18. codeforces水题100道 第十八题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table (brute force)
  19. bootstrap-table 切换页码保留勾选的checkbox
  20. 在 IE 浏览器中,使用 bootstrap 使得页面滚动条浮动显示,自动隐藏,自动消失

热门文章

  1. hdu 2044-2050 递推专题
  2. MVC - 12.HtmlHelper
  3. Linux用户和用户组的初步知识
  4. 在centos 6.9安装wordpress,浏览器不能访问问题
  5. 【笔试题】精选30道Java笔试题解答
  6. Exception异常处理机制
  7. 洛谷P1565牛宫
  8. HDU 6073 Matching In Multiplication(拓扑排序)
  9. 在spring中手动编写事务
  10. React 中组件的生命周期