更新时间:2022-02-10 22:45:19
mrjob是由Yelp创建的Python MapReduce库,它封装了Hadoop流,允许MapReduce应用程序以更加Pythonic的方式编写。 mrjob用纯Python编写多步MapReduce作业。使用mrjob编写的MapReduce作业可以在本地测试,在Hadoop集群上运行,或使用Amazon Elastic MapReduce(EMR)在云中运行。
使用mrjob编写MapReduce应用程序有许多好处:
虽然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选项。
$ python mr_job.py -r hadoop hdfs://input/input.txt
$ python mr_job.py -r emr s3://input-bucket/input.txt