python 实现MR

1. 看下本地的测试数据:

[root@hadoop Desktop]# cat tour.txt
air:23;hotel:34;nation:CHINA
air:35;hotel:46;nation:USA
air:36;hotel:47;nation:USA
air:26;hotel:37;nation:CHINA
air:33;hotel:44;nation:USA
air:34;hotel:45;nation:USA
air:25;hotel:36;nation:CHINA
air:24;hotel:35;nation:CHINA
[root@hadoop Desktop]# cat mapper.txt
cat: mapper.txt: No such file or directory


2. 设计mapper

[root@hadoop Desktop]# cat mapper.py
#!/usr/bin/python

import sys



for line in sys.stdin:
    airs=line.split(";")
    air=airs[0].split(":")
    hotel=airs[1].split(":")
    print hotel[0]+" "+hotel[1]
    print air[0]+" "+air[1]



3. 设计reducer

[root@hadoop Desktop]# cat reducer.py
#!/usr/bin/python

import sys

allsum={}
product=""

for line in sys.stdin:
    products=line.split()
    product=products[0]
    allsum[product]=allsum.get(product,0)+int(products[1])

for key,value in allsum.items():
    print key+" "+str(value)

4. 赋予mapper.py 和reducer.py 执行权限

5. 测试mapper

[root@hadoop Desktop]# cat tour.txt | ./mapper.py
hotel 34
air 23
hotel 46
air 35
hotel 47
air 36
hotel 37
air 26
hotel 44
air 33
hotel 45
air 34
hotel 36
air 25
hotel 35
air 24

5.测试reducer

[root@hadoop Desktop]# cat tour.txt | ./mapper.py | ./reducer.py
hotel 324
air 236


6.将测试数据上传到HDFS

 [root@hadoop Desktop]# hadoop fs -ls /usr/egencia/tour/tour.txt
/usr/hadoop/hadoop-1.2.1/libexec/../conf/hadoop-env.sh: line 59: export: `mapred.tasktracker.reduce.tasks.maximum=4': not a valid identifier
Warning: $HADOOP_HOME is deprecated.

Found 1 items
-rw-r--r--   1 root supergroup        224 2013-08-27 05:22 /usr/egencia/tour/tour.txt


7. 在hadoop 执行MR

[root@hadoop Desktop]# hadoop jar /usr/hadoop/hadoop-1.2.1/contrib/streaming/hadoop-streaming-1.2.1.jar -mapper /root/Desktop/mapper.py -reducer /root/Desktop/reducer.py -input /usr/egencia/tour -output /usr/egencia/tour/out

注意:执行的时候在指定mapper和reducer的时候不能像在本地测试一样采用./mapper.py 和./reducer.py ,这也是容易理解的,因为在将这两个文件拷贝到其他的datanode上去的时候,默认的执行目录不一定就是我在本地测试的desktop



8. HDFS 上查看结果:


[root@hadoop Desktop]# hadoop fs -cat /usr/egencia/tour/out/part-00000
/usr/hadoop/hadoop-1.2.1/libexec/../conf/hadoop-env.sh: line 59: export: `mapred.tasktracker.reduce.tasks.maximum=4': not a valid identifier
Warning: $HADOOP_HOME is deprecated.

hotel 324
air 236


可以看出结果和本地测试的是一样的,但是在python版本中怎么实现partitioner ,combiner,等还需考量,另外从上面的reducer'的函数也可以看出,其实根本就不会像java一样输入输出都是key、value而是自己可以随便定义,当然也不会自动将同一个key组合的一个reducer中作为一个迭代,所以在上面的reducer中药自己控制迭代,这些问题待解





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种分布式计算模型,Python可以通过Hadoop Streaming实现MapReduce功能。以下是一个使用Python实现的简单MapReduce示例: 假设我们有一个包含数字的文本文件,我们想要计算每个数字出现的次数。 首先,我们需要编写一个map.py脚本来实现map函数的功能。map.py的代码如下所示: ``` #!/usr/bin/env python import sys for line in sys.stdin: line = line.strip() words = line.split() for word in words: print('%s\t%s' % (word, 1)) ``` 这个脚本将输入的每一行文本拆分成单个数字,并将每个数字输出为键值对(数字,1)。 接下来,我们需要编写一个reduce.py脚本来实现reduce函数的功能。reduce.py的代码如下所示: ``` #!/usr/bin/env python 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 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 current_word == word: print('%s\t%s' % (current_word, current_count)) ``` 这个脚本将接收到的键值对按键进行排序,并且累加相同键的值。最后输出每个数字出现的次数。 最后,我们可以通过Hadoop Streaming来运行MapReduce作业。假设我们的输入文件名为input.txt,输出文件名为output.txt,我们可以使用以下命令来运行: ``` hadoop jar /path/to/hadoop-streaming.jar \ -file /path/to/map.py -mapper /path/to/map.py \ -file /path/to/reduce.py -reducer /path/to/reduce.py \ -input /path/to/input.txt -output /path/to/output.txt ``` 这个命令将使用MapReduce对输入文件进行处理,并将结果输出到指定的输出文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值