hadoop平台使用python编写mapreduce二次排序小程序

7 篇文章 0 订阅
4 篇文章 0 订阅

接上一个博文的环境
使用的是官网的专利使用数据,这里只截取了一部分

3858241,956203
3858241,1324234
3858241,3398406
3858241,3557384
3858241,3634889
3858242,1515701
3858242,3319261
3858242,3668705
3858242,3707004
3858243,2949611
3858243,3146465
3858243,3156927
3858243,3221341
3858243,3574238
3858243,3681785
3858243,3684611
3858244,14040
3858244,17445

mapper.py

  1 #!/usr/bin/env python
  2 import sys
  3 list1=[]
  4 for line in sys.stdin:
  5     line=line.strip()
  6     words = line.split("\n")
  7     list1.append(words[0])
  8 for x in xrange(len(list1)):
  9     print list1[x]

reducer.py

1 #!/usr/bin/env python
  2 from operator import itemgetter
  3 import sys
  4 dick1={}
  5 for line in sys.stdin:
  6     words = line.split("\n")
  7     if words[0][0]=='\t':
  8         continue
  9     else:
 10 
 11         word =words[0].split(",")
 12 #        print word[1]
 13 #        print "%s\t%s" % (word[0],word[1])     
 14         if str(word[0]) in dick1:
 15             dick1[word[0]].append(int((word[1].split("\t"[0]))[0]))
 16         else:
 17             dick1[word[0]]=[int((word[1].split("\t"[0]))[0])]
 18 for one in dick1:
 19     dick1[one].sort()  #进行单个键内对多个值的排序
 20 dick2=sorted(dick1.items(), key=lambda d:d[0], reverse = False)   #进行键的排序
 21 for one in dick2:
 22     print one

输出结果

[hadoop@mylab-client py]$ hadoop fs -cat out/part-00000
('3858241', [956203, 1324234, 3398406, 3557384, 3634889])   
('3858242', [1515701, 3319261, 3668705, 3707004])   
('3858243', [2949611, 3146465, 3156927, 3221341, 3574238, 3681785, 3684611])    
('3858244', [14040, 17445, 2211676])    
[hadoop@mylab-client py]$ 

再进行格式化输出即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值