大数据-hadoopMapReduce的mrjob实现

本文介绍了如何使用mrjob库来实现Hadoop MapReduce,包括WordCount、top-N等常见操作。详细讲解了如何通过inline和local方式在本地进行调试和运行,以及如何将任务提交到Hadoop集群,并设置任务优先级和任务数量。
摘要由CSDN通过智能技术生成

WordCount

from mrjob.job import MRJob

class MRWordCounter(MRJob):

    def mapper(self, key, line):
        for word in line.split():
            yield word, 1

    def reducer(self, word, occurrences):
        yield word, sum(occurrences)
        
if __name__=='__main__':
    MRWordCounter.run()

top-N

import sys
from mrjob.job import MRJob,MRStep
import heapq

class TopNWords(MRJob):

    def mapper(self, _, line):
        if line.strip() != "":
            for word in line.strip().split():
                yield word,1
                
    def combiner(self, word, counts):    #介于mapper和reducer之间,用于临时的将mapper输出的数据进行统计
        yield word,sum(counts)

    def reducer_sum(self, word, counts):
        yield None,(sum(counts),word)

    def top_n_reducer(self,_,word_cnts):    #利用heapq将数据进行排序,将最大的2个取出
        for cnt,word in heapq
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值