RDD编程初级实践(期末大作业)

1.pyspark交互式编程

请到教材官网的“下载专区”的“数据集”中下载chapter4-data1.txt,该数据集包含了某大学计算机系的成绩,数据格式如下所示:
Tom,DataBase,80
Tom,Algorithm,50
Tom,DataStructure,60
Jim,DataBase,90
Jim,Algorithm,60
Jim,DataStructure,80
……
请根据给定的实验数据,在pyspark中通过编程来计算以下内容:
【实验要求】

(1)该系总共有多少学生;

>>> lines = sc.textFile("file:///usr/local/spark/mycode/sparksqldata/data.txt")
>>> res = lines.map(lambda x :x.split(",")).map(lambda x :x[0])
>>> distinct_res = res.distinct()
>>> distinct_res.count()
265

(2)该系共开设了多少门课程;

>>> lines = sc.textFile("file:///usr/local/spark/mycode/sparksqldata/data.txt")
>>>> res = lines.map(lambda x :x.split(",")).map(lambda x :x[1])
>>> distinct_res = res.distinct()
>>> distinct_res.count()
8

(3)Tom同学的总成绩平均分是多少;

>>> lines = sc.textFile("file:///usr/local/spark/mycode/sparksqldata/data.txt")
>>> res = lines.map(lambda x :x.split(",")).filter(lambda x :x[0]=="Tom")
>>> score = res.map(lambda x :int(x[2]))
>>> num = res.count()
>>> score_sum = score.reduce(lambda x ,y: x+y)
>>> avg = score_sum/num
>>> print(avg)
30.8

(4)求每名同学的选修的课程门数;

>>> lines = sc.textFile("file:///usr/local/spark/mycode/sparksqldata/data.txt")
>>> res = lines.map(lambda x :x.split(",")).map(lambda x :(x[0],1))
>>> each_res = res.reduceByKey(lambda x,y:x+y)
>>> each_res.foreach(print)
('Abbott', 42)
('Abel', 56)
('Abraham', 42)

(5)该系DataBase课程共有多少人选修;

>>> lines = sc.textFile("file:///usr/local/spark/mycode/sparksqldata/data.txt")
>>> res = lines.map(lambda x :x.split(",")).filter(lambda x :x[1]=="DataBase")
>>> res.count()
1764

(6)各门课程的平均分是多少;

>>> lines = sc.textFile("file:///usr/local/spark/mycode/sparksqldata/data.txt")
>>> res = lines.map(lambda x :x.split(",")).map(lambda x : (x[1],(int(x[2]),1)))
>>> temp = res.reduceByKey(lambda x,y:(x[0]+y[0],x[1]+y[1]))
>>> avg = temp.map(lambda x :(x[0],round(x[1][0]/x[1][1],2)))
>>> avg.foreach(print)
('OperatingSystem', 54.94)
('Python', 57.82)
('ComputerNetwork', 51.9)
('DataBase', 50.54)
('Algorithm', 48.83)
('DataStructure', 47.57)
('CLanguage', 50.61)
('Software', 50.91)

(7)使用累加器计算共有多少人选了DataBase这门课。

>>> lines = sc.textFile("file:///usr/local/spark/mycode/sparksqldata/data.txt")
>>> res = lines.map(lambda x :x.split(",")).filter(lambda x :x[1]=="DataBase")
>>> accum = sc.accumulator(0)
>>> res.foreach(lambda x: accum.add(1))
>>> accum.value
1764

2.编写独立应用程序实现数据去重

对于两个输入文件A和B,编写Spark独立应用程序,对两个文件进行合并,并剔除其中重复的内容,得到一个新文件C。本文给出门课的成绩(A.txt、B.txt)下面是输入文件和输出文件的一个样例,供参考。
输入文件A的样例如下:

20200101 x
20200102 y
20200103 x
20200104 y
20200105 z
20200106 z

输入文件B的样例如下:

20200101 y
20200102 y
20200103 x
20200104 z
20200105 y

根据输入的文件A和B合并得到的输出文件C的样例如下:

20200101 x
20200101 y
20200102 y
20200103 x
20200104 y
20200104 z
20200105 y
20200105 z
20200106 z

1.环境准备

准备两个A和B文件内容

zhong@sparklearn:/usr/local/spark/mycode/remdup$ la
A  B  remdup.py  .remdup.py.swn  .remdup.py.swo  .remdup.py.swp
zhong@sparklearn:/usr/local/spark/mycode/remdup$ 

下载Python的第三方库

zhong@sparklearn:~$ pip install findspark
zhong@sparklearn:~$ pip install pyspark

2.假设当前目录为/usr/local/spark/mycode/remdup,在当前目录下新建一个remdup.py文件,复制下面代码;

import findspark
findspark.init() # 初始化找到本机安装的spark的环境
from pyspark import SparkContext
#初始化SparkContext
sc = SparkContext('local','remdup')

#加载两个文件A和B
lines1 = sc.textFile("file:///usr/local/spark/mycode/remdup/A")
lines2 = sc.textFile("file:///usr/local/spark/mycode/remdup/B")
#合并两个文件的内容 
lines = lines1.union(lines2)
 
#去重操作      
distinct_lines = lines.distinct()
 
#排序操作    
res = distinct_lines.sortBy(lambda x:x)
    
#将结果写入result文件中,repartition(1)的作用是让结果合并到一个文件中,不加的话会结果写入到两个文件    
res.repartition(1).saveAsTextFile("file:///usr/local/spark/mycode/result/file")

3.在目录/usr/local/spark/mycode/remdup/result下即可得到结果文件file。

zhong@sparklearn:/usr/local/spark/mycode$ cd result
zhong@sparklearn:/usr/local/spark/mycode/result$ ls
file
zhong@sparklearn:/usr/local/spark/mycode/result$ 

file中的文件内容是A和B合并后的C文件

20200101 x
20200102 y
20200103 x
20200104 y
20200105 z
20200106 z
20200107 x
20200108 y
20200109 x
20200110 y
20200111 z
20200112 z
……

3.编写独立应用程序实现求平均值问题

每个输入文件表示班级学生某个学科的成绩,每行内容由两个字段组成,第一个是学生名字,第二个是学生的成绩;编写Spark独立应用程序求出所有学生的平均成绩,并输出到一个新文件中。本文给出门课的成绩(Algorithm.txt、Database.txt、Python.txt),下面是输入文件和输出文件的一个样例,供参考。
Algorithm成绩:
小明 92
小红 87
小新 82
小丽 90
Database成绩:
小明 95
小红 81
小新 89
小丽 85
Python成绩:
小明 82
小红 83
小新 94
小丽 91
平均成绩如下:
(小红,83.67)
(小新,88.33)
(小明,89.67)
(小丽,88.67)

1.假设当前目录为/usr/local/spark/mycode/avgscore,将各科成绩保存在(Algorithm.txt、Database.txt、Python.txt)

zhong@sparklearn:/usr/local/spark/mycode/avgscore$ vim Algorithm.txt
zhong@sparklearn:/usr/local/spark/mycode/avgscore$ vim Database.txt
zhong@sparklearn:/usr/local/spark/mycode/avgscore$ vim Python.txt

2.在当前目录下新建一个avgscore.py,复制下面代码;

import findspark
findspark.init() # 初始化找到本机安装的spark的环境
from pyspark import SparkContext
#初始化SparkContext
sc = SparkContext('local',' avgscore')
 
#加载三个文件Algorithm.txt、Database.txt和Python.txt
lines1 = sc.textFile("file:///usr/local/spark/mycode/avgscore/Algorithm.txt")
lines2 = sc.textFile("file:///usr/local/spark/mycode/avgscore/Database.txt")
lines3 = sc.textFile("file:///usr/local/spark/mycode/avgscore/Python.txt")
 
#合并三个文件的内容
lines = lines1.union(lines2).union(lines3)
 
#为每行数据新增一列1,方便后续统计每个学生选修的课程数目。data的数据格式为('小明a', (92, 1))
data = lines.map(lambda x:x.split(" ")).map(lambda x:(x[0],(int(x[1]),1)))
 
#根据key也就是学生姓名合计每门课程的成绩,以及选修的课程数目。res的数据格式为('小明a', (269, 3))
res = data.reduceByKey(lambda x,y:(x[0]+y[0],x[1]+y[1]))
 
#利用总成绩除以选修的课程数来计算每个学生的每门课程的平均分,并利用round(x,2)保留两位小数
result = res.map(lambda x:(x[0],round(x[1][0]/x[1][1],2)))
 
#将结果写入result文件中,repartition(1)的作用是让结果合并到一个文件中,不加的话会结果写入到三个文件
result.repartition(1).saveAsTextFile("file:///usr/local/spark/mycode/avgscore/result")

3. 在目录/usr/local/spark/mycode/avgscore下执行下面命令执行程序

zhong@sparklearn:/usr/local/spark/mycode/avgscore$ python3 avgscore.py 

4.在目录/usr/local/spark/mycode/avgscore/result下即可得到结果文件file。

zhong@sparklearn:/usr/local/spark/mycode/avgscore$ ls                           
Algorithm.txt  avgscore.py  Database.txt  Python.txt  result
zhong@sparklearn:/usr/local/spark/mycode/avgscore$ cd result/
zhong@sparklearn:/usr/local/spark/mycode/avgscore/result$ ls
part-00000  _SUCCESS

参考:https://www.cnblogs.com/msq2000/p/12791889.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值