Python编写Hadoop MapReduce程序

adoop 的 MapReduce 程序,使用的是 Java ,但是使用 Java 很明显的一个弊端就是每次都要编码、打包、上传、执行,还真心是麻烦,想要更加简单的使用 Hadoop 的运算能力,想要写 MapReduce程序不那么复杂。还真是个问题。

仔细考虑了下,python刚好切合这个需求,随便搜了下 Python 编写 MapReduce程序,看了个教程,接下来就写下这篇博客做下记录。


Hadoop 框架使用 Java 开发的,对 Java 进行了原生的支持,不过对于其它语言也提供了 API 支持,如 Python 、 C++ 、 Perl 、 Ruby 等。在看一篇大神写的用脚本语言编写Hadoop基础程序时,需要用到一个工具,那这个工具就是 Hadoop Streaming ,顾名思义, Streaming 就是 Pipe 操作 。

 

前置条件:

Python 环境

Hadoop 环境( single or cluster )

 

最容易的 Hadoop 编程模型就是 Mapper 和 Reducer 的编写,这种编程模型大大降低了我们对于并发、同步、容错、一致性的要求,你只要编写好自己的业务逻辑,就可以提交任务。然后喝杯茶,结果就出来了,前提是你的业务逻辑没有错误。

使用 Hadoop Streaming ,能够利用 Pipe 模型,而使用 Python 的巧妙之处在于处理输入输出的数据使用的是 STDIN 和 STDOUT ,然后 Hadoop Streaming 会接管一切,转化成 MapReduce 模型。

 

我们还是使用 wordcount 例子,具体内容不再详细解释。下面我们先看下 mapper 的代码:

[python]  view plain copy
  1. #!/usr/bin/env python  
  2.   
  3. import sys  
  4. #input comes from STDIN (standard input)  
  5. for line in sys.stdin:  
  6.     # remove leading and trailing whitespace  
  7.     line = line.strip()  
  8.     # split the line into words  
  9.     words = line.split()  
  10.     # increase counters  
  11.     for word in words:  
  12.         # write the results to STDOUT (standard output);  
  13.         # what we output here will be the input for the  
  14.         # Reduce step, i.e. the input for reducer.py  
  15.         # tab-delimited; the trivial word count is 1  
  16.         
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值