python 的hadoop统计词频脚本实现

map阶段

# -*- coding: utf-8 -*-
import sys

for line in sys.stdin:
    line = line.strip()
    words = line.split()
    for word in words:
        print("%s	%s" % (word, 1))

reduce阶段

# -*- coding: utf-8 -*-
import sys

current_word = None
current_count = 0
word = None

for line in sys.stdin:
    word, count = line.split('	', 1)
    try:
        count = int(count)
    except ValueError:
        continue
    if current_word == word:
        current_count += count
    else:
        if current_word:
            print ('%s	%s' % (current_word, current_count))
        current_word = word
        current_count = count

if current_word == word:
    print('%s	%s' % (current_word, current_count))
原文地址:https://www.cnblogs.com/syw-home/p/13578296.html