python后端程序例子_Python MR程序示例

hadoop jar hadoop-streaming-2.6.4.jar \

-D mapreduce.job.name='test' \

-files /local/path/to/mapper.py,/local/path/to/reducer.py

-input /test/data/*

-output /test/output/

-mapper 'python /local/path/to/mapper.py'

-reducer 'python /local/path/to/reducer.py'

1. python文件需要分发到每个节点

2. -mapper和-reducer后面必须带python,否则会报错

Caused by: java.io.IOException: Cannot run program "mapper.py": error=2, No such file or director

mapper.py

#!/usr/bin/python3#-*- coding: utf-8 -*-

importosimportsysimportrefor line insys.stdin:

line=line.strip()

words= re.split('[,.?\s"]',line)for word inwords:

word= word.strip(',|.|?|\s')ifword:print("{0}\t{1}".format(word,1))

reducer.py

#!/usr/bin/env python#-*- coding: utf-8 -*-

importosimportsysfrom operator importitemgetter

current_word=None

current_count=0

word=Nonefor line insys.stdin:

word= line.split('\t',1)[0]

count= line.split('\t',1)[1]

count=int(count)if current_word ==word:

current_count+=countelse:ifcurrent_word:print("{0}\t{1}".format(current_word,current_count))

current_word=word

current_count=countifword:print("{0}\t{1}".format(current_word,current_count))

参考官方说明: https://hadoop.apache.org/docs/r2.7.7/hadoop-streaming/HadoopStreaming.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值