python版的mapreduce程序运行在hadoop上
来源:互联网 发布:网络销售微整形 编辑:程序博客网 时间:2024/06/09 17:39
通过标准输入(stdin)和标准输出(stdout)来传递Map和Reduce过程之间的数据。
1、数据文件
zhangsan 15lisi 15zhangsan 16lisi 16wangwu 14zhangsan 15lisi 16
2 map程序——mapper.py
#!/usr/bin/env python2.6import sysfor line in sys.stdin: line=line.strip() words=line.split() for word in words: print '%s\t%s' % (word,1)
确保该文件是可执行的:
chmod +x /TestData/mapper.py
另外第一行记得添加:#!/usr/bin/env python2.6
3、reduce程序——reducer.py
#!/usr/bin/env python2.6from operator import itemgetterimport syscurrent_word = Nonecurrent_count = 0word = None# input comes from STDINfor line in sys.stdin: # remove leading and trailing whitespace line = line.strip() # parse the input we got from mapper.py word, count = line.split('\t', 1) # convert count (currently a string) to int try: count = int(count) except ValueError: # count was not a number, so silently # ignore/discard this line continue # this IF-switch only works because Hadoop sorts map output # by key (here: word) before it is passed to the reducer if current_word == word: current_count += count else: if current_word: # write result to STDOUT print '%s\t%s' % (current_word, current_count) current_count = count current_word = word# do not forget to output the last word if needed!if current_word == word: print '%s\t%s' % (current_word, current_count)
确保该文件是可执行的:
chmod +x /TestData/reducer.py
5、单机测试
注:如果报错/usr/bin/env: python2.6: No such file or directory可能是从windows复制到linux上出问题了,在linux上重新写一份
6、hadoop上运行
hadoop jar /opt/cloudera/parcels/CDH-5.4.0-1.cdh5.4.0.p0.27/jars/hadoop-streaming-2.6.0-mr1-cdh5.4.0.jar -file /TestData/mapper.py -mapper /TestData/mapper.py -file /TestData/reducer.py -reducer /TestData/reducer.py -input /OutTestData/streamingtestdata.txt -output /OutTestData/streamingpython-output
运行如下:
运行结果:
0 0
- python版的mapreduce程序运行在hadoop上
- C++版的mapreduce程序运行在hadoop上(1)
- 在Hadoop上运行基于RMM中文分词算法的MapReduce程序
- hadoop上的两种运行mapreduce程序的方法
- hadoop的mapreduce运行在yarn上的原理
- python 运行 hadoop 2.0 mapreduce 程序
- 如何在Hadoop上编写MapReduce程序
- 如何在Hadoop上编写MapReduce程序
- 如何在Hadoop上编写MapReduce程序
- 如何在Hadoop上编写MapReduce程序
- 在 Hadoop 上编写 MapReduce 程序
- 如何在Hadoop上编写MapReduce程序
- 如何在Hadoop上编写MapReduce程序
- mapreduce程序在yarn上运行的流程
- 在Hadoop上运行python程序错误集合
- python在hadoop上运行
- 在eclipse上运行MapReduce程序
- 如何在Amazon Elastic MapReduce(EMR)上运行MapReduce程序
- php中$this->是什么意思
- 菜单悬浮
- C++ 头文件
- 黑马程序员——面向对象
- 使用JavaScript判断输入的内容是否为空,应该注意的一点!
- python版的mapreduce程序运行在hadoop上
- SVN:SVN学习的总结
- 使用Tornado实现http代理
- Android网络访问解决方案小结·1
- [LeetCode]Search a 2D Matrix II
- 可伸缩系统的架构经验
- 在字符串资源文件中添加HTML元素,直接使用字符串资源,HTML元素没起作用的解决办法
- 使用POI 导出excel表格
- Android的消息机制