python 实现 mapreducer
来源:互联网 发布:湖南银楼软件下载 编辑:程序博客网 时间:2024/05/20 04:50
一:mapper.py(map类)
import sysfor line in sys.stdin: line = line.strip() words = line.split() for word in words: print '%s\t%s' %(word,1)
二:reducer.pyimport syscurrent_word = Nonecurrent_count = 0word = Nonefor line in sys.stdin: line = line.strip() word,count = line.split('\t',1) try: count = int(count) except ValueError: 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 = wordif word == current_word: print '%s\t%s'(current_word,current_count)(将这两个python文件上传到master节点的root根目录下)
(注意将这两个python文件改权限)
#:chmod 777 mapper.py
#: chmod 777 reducer.py
三:测试代码
在root根目录下创建一个文件ddd.txt
#:vi ddd.txt
在ddd.txt中写入数据
ggg
ggg
hhh
hhh
aaa
保存文件,并退出
输入以下命令进行测试
[root@master ~]# more ddd.txt | python ./mapper.py | sort | python ./reduce
输出结果
aaa 1
ggg 2
hhh 2
四:实现hadoop的mapreduce
启动hadoop集群,在hdfs文件系统中建立一个文件夹为 input
hdfs dfs -mkdir /input
查看是否建立成功
hdfs dfs -ls -R /
将ddd.txt文件上传到 input目录下
hdfs dfs -put /root/ddd.txt /input
输入以下命令
#:/usr/local/hadoop/bin/hadoop jar /usr/local/hadoop/share/hadoop/tools/lib/hadoop-streaming-2.7.0*.jar
-D stream.non.zero.exit.is.failure=false -files 'mapper.py,ccc.py' -input /input/dat0203.log -output /zanni -mapper "python ./mapper.py" -reducer "python ./ccc.py"
任务完成,到hdfs文件系统的输出目录中,可以看到输出文件
阅读全文
0 0
- python 实现 mapreducer
- MapReducer实现单词计算
- MapReducer浅析
- MapReducer优化
- Hadoop-MapReducer 利用计数器Counters(Java)和Error output(python)计数
- MapReducer的Job调优
- MapReducer-找共同好友
- MapReducer程序调试技巧
- MapReducer入门案例MyWordCount
- MapReducer工作流程小结
- Mapreducer任务启动
- [Hadoop]MapReducer工作过程
- MapReducer中文编码
- MapReducer多表查询
- 两表MapReducer
- Hadoop MapReducer工作过程
- Hadoop客户端 mapreducer
- MapReducer流量统计
- C++ REST SDK(C++语言客户端-服务器通信库)
- (QT)QT跳转页面(信号-槽机制)
- HDU 2844:Coins (完全背包)
- 【C#】常量
- 数据结构
- python 实现 mapreducer
- c++中string erase函数的使用
- python常用模块
- sql 中 ${} 和 #{}的区别
- java pdf文件转 图片
- main函数参数
- HBase最佳实践-HBase中的读性能优化策略
- Code Sign Error in macOS Sierra Xcode 8.3.3
- 二叉树先序遍历,中序遍历,后序遍历,层次遍历。