python 编写函数例子_用python写MapReduce函数——以WordCount为例

尽管Hadoop框架是用java写的,但是Hadoop程序不限于java,可以用python、C++、ruby等。本例子中直接用python写一个MapReduce实例,而不是用Jython把python代码转化成jar文件。

例子的目的是统计输入文件的单词的词频。

输入:文本文件

输出:文本(每行包括单词和单词的词频,两者之间用'\t'隔开)

1. Python MapReduce 代码

使用python写MapReduce的“诀窍”是利用Hadoop流的API,通过STDIN(标准输入)、STDOUT(标准输出)在Map函数和Reduce函数之间传递数据。

我们唯一需要做的是利用Python的sys.stdin读取输入数据,并把我们的输出传送给sys.stdout。Hadoop流将会帮助我们处理别的任何事情。

1.1 Map阶段:mapper.py

在这里,我们假设把文件保存到hadoop-0.20.2/test/code/mapper.py

#!/usr/bin/env python

importsysfor line insys.stdin:

line=line.strip()

words=line.split()for word inwords:print "%s\t%s" % (word, 1)

文件从STDIN读取文件。把单词切开,并把单词和词频输出STDOUT。Map脚本不会计算单词的总数,而是输出 1。在我们的例子中,我们让随后的Reduce阶段做统计工作。

为了是脚本可执行,增加mapper.py的可执行权限

chmod +x hadoop-0.20.2/test/code/mapper.py

1.2 Reduce阶段:reducer.py

在这里,我们假设把文件保存到hadoop-0.20.2/test/code/reducer.py

#!/usr/bin/env python

from operator importitemgetterimportsys

current_word=None

current_count=0

word=Nonefor line insys.stdin:

line=line.strip()

word, count= line.split('\t', 1)try:

count=int(count)exceptValueError: #count如果不是数字的话,直接忽略掉continue

if current_word ==word:

current_count+=countelse:ifcurrent_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)

文件会读取mapper.py 的结果作为reducer.py 的输入,并统计每个单词出现的总的次数,把最终的结果输出到STDOUT。

为了是脚本可执行,增加reducer.py的可执行权限

chmod +x hadoop-0.20.2/test/code/reducer.py

细节:split(chara, m),第二个参数的作用,下面的例子很给力

str = 'server=mpilgrim&ip=10.10.10.10&port=8080'

print str.split('=', 1)[0] #1表示=只截一次print str.split('=', 1)[1]print str.split('=')[0]print str.split('=')[1]

输出

server

mpilgrim&ip=10.10.10.10&port=8080

server

mpilgrim&ip

1.3 测试代码(cat data | map | sort | reduce)

这里建议大家在提交给MapReduce job之前在本地测试mapper.py 和reducer.py脚本。否则jobs可能会成功执行,但是结果并非自己想要的。

功能性测试mapper.py 和 reducer.py

[rte@hadoop-0.20.2]$cd test/code

[rte@code]$echo "foo foo quux labs foo bar quux" | ./mapper.py

foo1

foo1

quux1

labs1

foo1

bar1

quux1

[rte@code]$echo "foo foo quux labs foo bar quux" | ./mapper.py | sort -k1,1 | ./reducer.py

bar1

foo3

labs1

quux2

细节:sort -k1,1 参数何意?

-k, -key=POS1[,POS2] 键以pos1开始,以pos2结束

有时候经常使用sort来排序,需要预处理把需要排序的field语言在最前面。实际上这是

完全没有必要的,利用-k参数就足够了。

比如sort all

1 4

2 3

3 2

4 1

5 0

如果sort -k 2的话,那么执行结果就是

5 0

4 1

3 2

2 3

1 4

2. 在Hadoop上运行python代码

2.1 数据准备

下载以下三个文件的

我把上面三个文件放到hadoop-0.20.2/test/datas/目录下

2.2 运行

把本地的数据文件拷贝到分布式文件系统HDFS中。

bin/hadoop dfs -copyFromLocal /test/datas hdfs_in

查看

bin/hadoop dfs -ls

结果

drwxr-xr-x - rte supergroup 0 2014-07-05 15:40 /user/rte/hdfs_in

查看具体的文件

bin/hadoop dfs -ls /user/rte/hdfs_in

执行MapReduce job

bin/hadoop jar contrib/streaming/hadoop-*streaming*.jar \-file test/code/mapper.py -mapper test/code/mapper.py \-file test/code/reducer.py -reducer test/code/reducer.py \-input /user/rte/hdfs_in/*-output /user/rte/hdfs_out

实例输出

052056273711151.jpg

查看输出结果是否在目标目录/user/rte/hdfs_out

bin/hadoop dfs -ls /user/rte/hdfs_out

输出

Found 2 items

drwxr-xr-x - rte supergroup 0 2014-07-05 20:51 /user/rte/hdfs_out2/_logs

-rw-r--r-- 2 rte supergroup 880829 2014-07-05 20:51 /user/rte/hdfs_out2/part-00000

查看结果

bin/hadoop dfs -cat /user/rte/hdfs_out2/part-00000

输出

052100394491796.jpg

以上已经达成目的了,但是可以利用python迭代器和生成器优化

3. 利用python的迭代器和生成器优化Mapper 和 Reducer代码

3.1 python中的迭代器和生成器

3.2 优化Mapper 和 Reducer代码

mapper.py

#!/usr/bin/env python

importsysdefread_input(file):for line infile:yieldline.split()def main(separator='\t'):

data=read_input(sys.stdin)for words indata:for word inwords:print "%s%s%d" % (word, separator, 1)if __name__ == "__main__":

main()

reducer.py

#!/usr/bin/env python

from operator importitemgetterfrom itertools importgroupbyimportsysdef read_mapper_output(file, separator = '\t'):for line infile:yield line.rstrip().split(separator, 1)def main(separator = '\t'):

data= read_mapper_output(sys.stdin, separator =separator)for current_word, group ingroupby(data, itemgetter(0)):try:

total_count= sum(int(count) for current_word, count ingroup)print "%s%s%d" %(current_word, separator, total_count)exceptvalueError:pass

if __name__ == "__main__":

main()

细节:groupby

from itertools importgroupbyfrom operator importitemgetter

things= [('2009-09-02', 11),

('2009-09-02', 3),

('2009-09-03', 10),

('2009-09-03', 4),

('2009-09-03', 22),

('2009-09-06', 33)]

sss=groupby(things, itemgetter(0))for key, items insss:printkeyfor subitem initems:printsubitemprint '-' * 20

结果

>>>

2009-09-02

('2009-09-02', 11)

('2009-09-02', 3)

--------------------

2009-09-03

('2009-09-03', 10)

('2009-09-03', 4)

('2009-09-03', 22)

--------------------

2009-09-06

('2009-09-06', 33)

--------------------

groupby(things, itemgetter(0)) 以第0列为排序目标

groupby(things, itemgetter(1))以第1列为排序目标

groupby(things)以整行为排序目标

4. 参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值