[MapReduce]Top 10 标签

源自Udacity,Intro to hadoop这门课Final Project

题目要求,从给定的论坛帖子数据中,找出被使用最多的10个tag。tag存在于question中(因为comment和answer也存在于数据表中,所有需要在mapper中过滤)

思路:
mapper 从是question的记录中,提取tags,并输出
reducer 给每个tags计数,保存到一个dict里面,最后排序输出 


# mapper
#!/usr/bin/python

import sys
import csv

reader = csv.reader(sys.stdin, delimiter = "\t")
next(reader, None)

for line in reader:
    if len(line) != 19:
        continue

    node_type = line[5].strip()
    if node_type == "question":
        tags = line[2].strip().split()
        for tag in tags:
            print tag

# reducer
#!/usr/bin/python

import sys
import csv

tags = {}
oldKey = None
count = 0

for line in sys.stdin:
    data = line.strip().split("\t")
    if len(data) != 1:
        continue

    thisKey = data[0]

    if oldKey and thisKey != oldKey:
        tags[oldKey] = count
        count = 0
        oldKey = thisKey

    oldKey = thisKey
    count += 1

if oldKey != None:
    tags[oldKey] = count

top10 = sorted(tags, key=tags.get, reverse=True)[:10]
for tag in top10:
    print "{0}\t{1}".format(tag, tags[tag])


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值