MapReduce求各年销售笔数,各年销售总额

实验名称

求各年销售笔数,各年销售总额

实验目的

1、熟练理解和掌握MapReduce编程模型

2、掌握使用其他语言实现MapReduce

实验原理

http://hadoop.apache.org/docs/r1.0.4/cn/streaming.html

实验环境

Ubuntu 16.04
Hadoop 2.7.3

Python 3.6

实验要求

自行编写map.py代码,注意要点:

1、对空白行的处理,可判断 line[0] != ‘’

2、时间数据包含年月日,只需截取年份

3、可以将每行订单数据都算为1笔

4、目标输出格式:年份,数额

代码粘贴:

import sys
   
for line in sys.stdin:
           if line[0]!="" :
            col = line.strip().split(',')
            col1=col[2].strip().split('-')
            col[2]=col1[0]
            print(col[2],',',col[6])

自行编写reduce.py代码,注意要点

1、可以将数据的行数当成笔数

2、输出时应按年份排好序

3、目标输出格式: 年份,笔数-总额

(总额要求转化成k且保留两位小数,如13572468.98 -> 13572.46k)

代码粘贴:

#!/bin/env python
# encoding: utf-8
from operator import itemgetter
import itertools
import sys


def read_mapper_output(file, separator = ','):
    for line in file:
       yield line.rstrip().split(separator,1)
stdin_generator=read_mapper_output(sys.stdin, ',')
for year, sals in itertools.groupby(stdin_generator,itemgetter(0) ):
  count=0
  total_sal=0
  for year,cur_sal in sals:
      count = count+1
      total_sal=total_sal + float(cur_sal)
  print(year,'\t',count,'\t','%.2f'%(total_sal/1000))

本地管道测试

cat sales.csv | python map.py | sort -k 1 | python reduce.py
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

log@#

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值