2021-06-05

RDD编程初级实践
一、实验目的
(1)熟悉Spark的RDD基本操作及键值对操作;
(2)熟悉使用RDD编程解决实际具体问题的方法。
二、实验平台
操作系统:Ubuntu16.04
Spark版本:2.4.0
Python版本:3.4.3
三、实验内容和要求
1.pyspark交互式编程
本作业提供分析数据data.txt,该数据集包含了某大学计算机系的成绩,数据格式如下所示:
Tom,DataBase,80
Tom,Algorithm,50
Tom,DataStructure,60
Jim,DataBase,90
Jim,Algorithm,60
Jim,DataStructure,80
……
请根据给定的实验数据,在pyspark中通过编程来计算以下内容:
将所需用到的数据导入到一个文件夹里,以便接下来的实验。
hadoop@dblab-VirtualBox:/usr/local/spark$ cd /usr/local/spark/bigwork
hadoop@dblab-VirtualBox:/usr/local/spark/bigwork$ ls
可以看到所需用到的data.txt在bigwork这个文件夹下。

启动pyspark,在spark中来完成接下来的问题。
hadoop@dblab-VirtualBox:/usr/local/spark/bigwork$ pyspark

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

lines = sc.textFile(‘file:///usr/local/spark/bigwork/data.txt’)
res = lines.map(lambda x:x.split(",")).map(lambda x:x[0])
sum = res.distinct()
sum.count()
结果为:265个
(2)该系共开设了多少门课程;

res = lines.map(lambda x:x.split(",")).map(lambda x:x[1])
dis_res = res.distinct()
dis_res.count()
结果为:8门
(3)Tom同学的总成绩平均分是多少;

res = lines.map(lambda x:x.split(",")).filter(lambda x:x[0]==‘Tom’)
res.foreach(print)
num = res.count()
score = res.map(lambda x:int(x[2]))
sum_score = score.reduce(lambda x,y:x+y)
avg = sum_score / num
print(avg)
结果为:30.8分
(4)求每名同学的选修的课程门数;

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)
结果为:
(5)该系DataBase课程共有多少人选修;

res = lines.map(lambda x:x.split(",")).filter(lambda x:x[1]==“DataBase”)
res.count()
结果为:1764人
(6)各门课程的平均分是多少;

res = lines.map(lambda x:x.split(",")).map(lambda x:(x[1],(int(x[2]),1)))
tem = res.reduceByKey(lambda x,y:(x[0]+y[0],x[1]+y[1]))
avg = tem.map(lambda x:(x[0],round(x[1][0]/x[1][1],2)))
avg.foreach(print)
结果为:
(7)使用累加器计算共有多少人选了DataBase这门课。

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.txt与文件B.txt两个文件里的内容:

编写一个去重复内容的代码:
hadoop@dblab-VirtualBox:/usr/local/spark/bigwork$ vim Linjiaze.py
from pyspark import SparkContext
sc = SparkContext(‘local’,‘mycode/bigwork’)
lines1 = sc.textFile(‘file:///usr/local/spark/mycode/bigwork/A.txt’)
lines2 = sc.textFile(‘file:///usr/local/spark/mycode/bigwork/B.txt’)
lines = lines1.union(lines2)
distinct_lines = lines.distinct()
res = distinct_lines.sortBy(lambda x:x)
res.repartiton(1).saveAsTextFile(‘file:///usr/local/spark/mycode/bigwork/C’)
运行代码,查看结果:
hadoop@dblab-VirtualBox:/usr/local/spark/bigwork$ python3 Linjiaze.py
hadoop@dblab-VirtualBox:/usr/local/spark/bigwork$ cd /usr/local/spark/mycode/bigwork
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork$ ls
A.txt B.txt C
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork$ cd C/
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork/C$ ls
part-00000 _SUCCESS
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork/C$ vim part-00000
结果为:
3.编写独立应用程序实现求平均值问题
每个输入文件表示班级学生某个学科的成绩,每行内容由两个字段组成,第一个是学生名字,第二个是学生的成绩;编写Spark独立应用程序求出所有学生的平均成绩,并输出到一个新文件中。本文给出门课的成绩(Algorithm.txt、Database.txt、Python.txt),下面是输入文件和输出文件的一个样例。
导入并查看Algorithm.txt文件、Database.txt文件和Python.txt文件:

编写实现平均值的代码:
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork$ vim Linjiaze2.py
from pyspark import SparkContext
sc = SparkContext(‘local’,‘mycode/bigwork’)
lines1 = sc.textFile(‘file:///usr/local/spark/mycode/bigwork/Algorithm.txt’)
lines2 = sc.textFile(‘file:///usr/local/spark/mycode/bigwork/Database.txt’)
lines3 = sc.textFile(‘file:///usr/local/spark/mycode/bigwork/Python.txt’)
lines = lines1.union(lines2).union(lines3)
bigwork = lines.map(lambda x:x.split(" ")).map(lambda x:(x[0],(int(x[1]),1)))
res = bigwork.reduceByKey(lambda x,y:(x[0]+y[0],x[1]+y[1]))
result = res.map(lambda x:(x[0],round(x[1][0]/x[1][1],2)))
result.repartition(1).saveAsTextFile(‘file:///usr/local/sprak/mycode/bigwork/result3’)
运行代码,查看结果:
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork$ python3 Linjiaze2.py
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork$ ls
Algorithm.txt A.txt B.txt C Database.txt Linjiaze2.py Python.txt result3
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork$ cd result3/
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork/result3$ ls
part-00000 _SUCCESS
hadoop@dblab-VirtualBox:/usr/local/spark/mycode/bigwork/result3$ vim part-00000
结果为:

4.总结
通过此次对RDD初级编程实践的大数据大作业的完成,加上在这一学期的课程中在老师的指导下的关于一些RDD的实操实验,逐步的了解了RDD编程实践在平常的编程中的作用,再加上对各方面中RDD编程的练习和去重复以及通过代码实现多个文件的统计平均值的实验,逐渐加深了对RDD编程实践的理解和应用,在以后的生活中,会继续努力,继续钻研关于这方面的内容。

参考文献
[14]詹玲。面向应用的对象存储设备的数据组织研究[D].华中科技大学2009
[15]姚杰。分布式存储系统文件级连续数据保护技术研究[D].华中科技大2009
[16]孔君华。高钢级X80管线钢工艺、组织与性能的研究[D].华中科技大学2005

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值