Hadoop 3.0.0-alpha2安装(三)之Python编写MapReduce
2017-04-05 18:14
441 查看
4.5.4 Python编写MapReduce
# mkdir -p /data/test && cd /data/test# vim input.txt
foo foo quux labs foo bar quux abx bar see you bytest welcome test abc labs foo me python hadoop ab ac bc bec python
# vim mapper.py # 刘天斯 著《Python 自动化运维》中的代码
#!/usr/bin/env python
import sys
for line in sys.stdin:
line =line.strip()
words =line.split()
for word inwords:
print'%s\t%s' % (word, 1)
# vim reducer.py # 刘天斯 著《Python 自动化运维》中的代码
#!/usr/bin/env python
from operator import itemgetter
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:
continue
ifcurrent_word == word:
current_count += count
else:
ifcurrent_word:
print '%s\t%s' % (current_word, current_count)
current_count = count
current_word = word
if current_word == word:
print'%s\t%s' % (current_word, current_count)
# chmod +x *.py
# cat input.txt |./mapper.py # 测试py文件
# cat input.txt |./mapper.py |sort -k1,1|./reducer.py
$ hdf dfs -mkdir /user/hduser/input/word
$ hdfs dfs -put /data/test/input.txt/user/hduser/input/word
$ hadoop jar../share/hadoop/tools/lib/hadoop-streaming-3.0.0-alpha2.jar -file/data/test/mapper.py -mapper /data/test/mapper.py -file /data/test/reducer.py-reducer /data/test/reducer.py -input /user/hduser/input/word -output/user/hadoop/word
$ hdfs dfs -ls /user/hadoop/word # 查看生成的文件
Found 2 items
-rw-r--r-- 2hadoop supergroup 0 2017-04-0509:53 /user/hadoop/word/_SUCCESS
-rw-r--r-- 2hadoop supergroup 117 2017-04-0509:53 /user/hadoop/word/part-00000
$ hdfs dfs -cat/user/hadoop/word/part-00000
ab 1
abc 1
abx 1
ac 1
bar 2
bc 1
bec 1
by 1
foo 4
hadoop 1
labs 2
me 1
python 2
quux 2
see 1
test 2
welcomne 1
you 1
### good 成功了,继续努力……
### 以上python代码也可以这里看到:http://www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/
相关文章推荐
- Hadoop的安装方法,以及Eclipse编写MapReduce的配置,
- Hadoop 3.0.0-alpha2安装(一)
- 用Python编写Hadoop下MapReduce程序
- Hadoop:使用原生python编写MapReduce来统计文本文件中所有单词出现的频率功能
- 如何使用Python为Hadoop编写一个简单的MapReduce程序
- Python之——用Mrjob框架编写Hadoop MapReduce程序(基于Hadoop 2.5.2)
- 如何使用Python为Hadoop编写一个简单的MapReduce程序(这个人T字还有好几篇精华的可以看)
- Centos 7 安装Hadoop 3.0.0-alpha1
- hadoop 3.0.0 alpha3 安装、配置
- Hadoop 3.0.0-alpha2安装(二)
- Python编写Hadoop MapReduce程序
- 小白学习大数据之路——Hadoop3.0.0-alpha2 安装以及测试程序wordcount实践
- hadoop 3.0.0 alpha3 安装、配置
- hadoop 3.0.0 alpha3 安装、配置
- Python之——使用原生Python编写Hadoop MapReduce程序(基于Hadoop 2.5.2)
- hadoop平台使用python编写mapreduce排序小程序
- 如何使用Python为Hadoop编写一个简单的MapReduce程序
- Hadoop:使用原生python编写MapReduce
- hadoop2.7.1中用原生python编写mapreduce
- 如何使用Python为Hadoop编写一个简单的MapReduce程序