WordCount Python版(整理)

24 篇文章 2 订阅
16 篇文章 0 订阅

突然间想起来,之前用过Python版的WordCount,之前没有做整理,现在想想还不晚,整理一下,说不定以后还会用到。

MapReduce我最近用的不多了,但是感觉不少业务场景,都可在WordCount 的基础上改进实现。

Python 具体实现(一个shell 脚本、一个Python脚本):

##############################################################
# File Name: wordCount.sh
# Author: 
# mail: 
#=============================================================
#!/usr/bin/bash

cd `dirname $0`

source ~/.bashrc


in_dir=/user/lyx/input/*
out_dir=/user/lyx/output/

hadoop fs -rm -r -skipTrash $out_dir

hadoop jar $HADOOP_STREAMING \
				-D mapreduce.job.name="word count test" \
				-D mapreduce.job.queuename=root.xxxxxxxxxxxxx \
				-D mapred.map.tasks=500 \
				-D mapred.reduce.tasks=1000 \
				-input ${in_dir} \
				-output ${out_dir} \
				-file wordCount.py \
				-mapper "python wordCount.py mapper" \
				-reducer "python wordCount.py reducer"
##############################################################
# -*- coding=utf-8 -*-
# File Name: wordCount.py
# Author: 
# mail: 
# =============================================================
# !/usr/bin/python

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import sys


def mapper():
    for line in sys.stdin:
        lsp = line.strip().split()
        for word in lsp:
            print(word + "\t" + str(1))


def reducer():
    current_key = None
    current_count = 0

    for line in sys.stdin:
        lsp = line.strip().split("\t")
        if len(lsp) < 2:
            continue

        key = lsp[0]
        count = int(lsp[1])

        if current_key == key:
            current_count += count
        else:
            if current_key:
                print(current_key + "\t" + str(current_count))
            current_key = key
            current_count = count
    print(current_key + "\t" + str(current_count))


if __name__ == "__main__":
    if sys.argv[1] == "mapper":
        mapper()
    elif sys.argv[1] == "reducer":
        reducer()

声明: 总结学习,有问题或不当之处,可以批评指正哦,谢谢。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值