# RDD编程初级实践

2021-06-07 21:01:30云计算人已围观

### RDD编程初级实践

• 一、pyspark交互式编程
• 二、编写独立应用程序实现数据去重
• 三、编写独立应用程序实现求平均值问题

# 一、pyspark交互式编程

``````Tom,DataBase,80
Tom,Algorithm,50
Tom,DataStructure,60
Jim,DataBase,90
Jim,Algorithm,60
Jim,DataStructure,80
……
``````

（1）该系总共有多少学生；

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

（2）该系共开设了多少门课程；

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

（3）Tom同学的总成绩平均分是多少；

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

（4）求每名同学的选修的课程门数；

``````lines = sc.textFile("file:///usr/local/spark/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)
``````

（5）该系DataBase课程共有多少人选修；

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

（6）各门课程的平均分是多少；

``````lines = sc.textFile("file:///usr/local/spark/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)
``````

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

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

# 二、编写独立应用程序实现数据去重

``````20200540	y
20200541	x
20200542	y
20200543	z
20200544	z
...
``````

``````20200138	y
20200139	x
20200140	y
20200141	z
20200142	z
...
``````

``````20200138	y
20200139	x
20200140	y
20200141	z
20200142	z
20200143	x
20200144	y
20200145	x
20200146	y
20200147	z
...
``````

1，假设当前目录为/usr/local/spark/sparksqldata，在当前目录下新建一个remdup.py文件，复制下面代码；

``````from pyspark import SparkContext
sc = SparkContext('local','remdup')
lines1 = sc.textFile("file:///usr/local/spark/sparksqldata/A")
lines2 = sc.textFile("file:///usr/local/spark/sparksqldata/B")
lines = lines1.union(lines2)
distinct_lines = lines.distinct()
res = distinct_lines.sortBy(lambda x:x)
res.repartition(1).saveAsTextFile("file:///usr/local/spark/sparksqldata/result/file")
``````

2，最后在目录/usr/local/spark/sparksqldata下执行下面命令执行程序（注意执行程序时请先退出pyspark shell，否则会出现“地址已在使用”的警告）；

``````python3 remdup.py
``````

3，在目录/usr/local/spark/sparksqldata/result/file下即可得到结果文件part-00000。

# 三、编写独立应用程序实现求平均值问题

Algorithm成绩：

``````小明a 92

...
``````

Database成绩：

``````小明a 92

...
``````

Python成绩：

``````小明a 84

...
``````

``````('小明n', 90.5)
('小新z', 99.0)
('小丽f', 88.0)
('小新y', 76.0)
('小丽c', 89.0)
('小明v', 88.0)
('小新c', 76.0)
('小红n', 87.0)
('小新e', 95.0)
('小明f', 81.0)
('小明b', 89.0)
('小明t', 89.0)
('小新b', 75.0)
...
``````

1，假设当前目录为/usr/local/spark/sparksqldata，在当前目录下新建一个avgscore.py文件，复制下面代码；

``````from pyspark import SparkContext
sc = SparkContext('local',' avgscore')
lines1 = sc.textFile("file:///usr/local/spark/sparksqldata/Algorithm.txt")
lines2 = sc.textFile("file:///usr/local/spark/sparksqldata/Database.txt")
lines3 = sc.textFile("file:///usr/local/spark/sparksqldata/Python.txt")
lines = lines1.union(lines2).union(lines3)
distinct_lines = lines.distinct()
lines4 = distinct_lines.sortBy(lambda x:x !="")//去除空行避免后面报错
data = lines4.map(lambda x:x.split(" ")).map(lambda x:(x[0],(int(x[1]),1)))
res = data.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/spark/sparksqldata/result1")
``````

2，最后在目录/usr/local/spark/sparksqldata下执行下面命令执行程序（注意执行程序时请先退出pyspark shell，否则会出现“地址已在使用”的警告）；

``````python3 avgscore.py
``````

3，在目录/usr/local/spark/sparksqldata/result1下即可得到结果文件part-00000。

Tags：spark

## 文章评论

 留言与评论（共有 0 条评论）

 验证码：

## 站点信息

• 文章统计247968篇文章
• 浏览统计18356次浏览
• 评论统计1个评论
• 标签管理标签云
• 统计数据：统计代码
• 微信公众号：扫描二维码，关注我们