您现在的位置是:首页 > 文章详情

[雪峰磁针石博客]大数据Hadoop工具python教程4-mrjob

日期:2019-01-27点击:429

mrjob是由Yelp创建的Python MapReduce库,它封装了Hadoop流,允许MapReduce应用程序以更加Pythonic的方式编写。 mrjob用纯Python编写多步MapReduce作业。使用mrjob编写的MapReduce作业可以在本地测试,在Hadoop集群上运行,或使用Amazon Elastic MapReduce(EMR)在云中运行。

使用mrjob编写MapReduce应用程序有许多好处:

  • mrjob目前是非常活跃的框架,每周都有多次提交。
  • mrjob拥有丰富的文档。
  • 可以在不安装Hadoop的情况下执行和测试mrjob应用程序,在部署到Hadoop集群之前就可开发和测试。
  • mrjob允许MapReduce应用程序在单个类中编写,而不是为mapper和reducer编写单独的程序。

虽然mrjob是很好的解决方案,但它确实有它的缺点。 mrjob是简化的,因此它不会提供与其他API提供的Hadoop相同级别的访问权限。 mrjob不使用typedbytes,因此其他库可能更快。

安装

$ pip install mrjob

参考资料

单词统计

#!/usr/bin/env python # 项目实战讨论QQ群630011153 144081101 # https://github.com/china-testing/python-api-tesing from mrjob.job import MRJob class MRWordCount(MRJob): def mapper(self, _, line): for word in line.split(): yield(word, 1) def reducer(self, word, counts): yield(word, sum(counts)) if __name__ == '__main__': MRWordCount.run()

执行结果

$ python word_count.py /home/hduser_/input2.txt No configs found; falling back on auto-configuration No configs specified for inline runner Running step 1 of 1... Creating temp directory /tmp/word_count.hduser_.20190122.035729.128110 job output is in /tmp/word_count.hduser_.20190122.035729.128110/output Streaming final output from /tmp/word_count.hduser_.20190122.035729.128110/output... "nimble" 1 "be" 2 "quick" 1 "jack" 2 Removing temp directory /tmp/word_count.hduser_.20190122.035729.128110...

比较重要的方法有:mapper()、combiner()和reducer()。

多个输入文件:

$ python mr_job.py input1.txt input2.txt input3.txt

默认情况下,mrjob在本地运行,允许在提交到Hadoop集群之前开发和调试代码。
要更改作业的运行方式,请指定-r/--runner选项。

图片.png

$ python mr_job.py -r hadoop hdfs://input/input.txt $ python mr_job.py -r emr s3://input-bucket/input.txt
原文链接:https://yq.aliyun.com/articles/689110
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章