05 RDD编程

一、词频统计:

  1. 读文本文件生成RDD lines
lines=sc.textFile("file:///usr/local/spark/mycode/rdd/word.txt")
lines.foreach(print)

  1. 将一行一行的文本分割成单词 words flatmap()
words=lines.flatMap(lambda line:line.split())
words.foreach(print)

  1. 全部转换为小写 lower()
words1=lines.map(lambda word:word.lower())
words1.foreach(print)

  1. 去掉长度小于3的单词 filter()
word=words.filter(lambda words:len(words)>2)
word.foreach(print)

  1. 去掉停用词
with open("/usr/local/spark/mycode/rdd/stopwords.txt") as f:
stops=f.read().split()
lines.flatMap(lambda line:line.split()).filter(lambda word:word not in stops).collect()

  1. 转换成键值对 map()

words.map(lambda word:(word,1)).collect()

  1. 统计词频 reduceByKey()

words.map(lambda word:(word,1)).reduceByKey(lambda a,b:a+b).collect()

  1. 按字母顺序排序

words.map(lambda word : (word,1)).reduceByKey(lambda a,b:a+b).sortBy(lambda word:word[0]).collect()

  1. 按词频排序

words.map(lambda word:(word.lower(),1)).reduceByKey(lambda a,b:a+b).sortBy(lambda word:word[1],False).collect()

  1. 结果文件保存 saveAsTextFile(out_url)
lines=sc.textFile("file:///usr/local/spark/mycode/rdd/word.txt")
text = lines.map(lambda line:line.split(',')).map(lambda x:(x[1],(int(x[2]),1))).reduceByKey(lambda a,b:(a[0]+b[0],a[1]+b[1]))
text.saveAsTextFile("file:///home/hadoop/save")

二、学生课程分数案例

  1. 总共有多少学生?map(), distinct(), count()
lines=sc.textFile("file:///usr/local/spark/mycode/rdd/chapter4-data01.txt")
lines.map(lambda line:line.split(',')[0]).distinct().count()

  1. 开设了多少门课程?

lines.map(lambda line:line.split(',')[1]).distinct().count()

  1. 每个学生选修了多少门课?map(), countByKey()

name = lines.map(lambda line:line.split(',')).map(lambda line:(line[0],(line[1],line[2])))

name.take(5)

name.count()

name.countByKey()

  1. 每门课程有多少个学生选?map(), countByValue()
name=lines.map(lambda line:line.split(',')).map(lambda line:line[1])
name.countByValue()

  1. Tom选修了几门课?每门课多少分?filter(), map() RDD
Tom=lines.filter(lambda line:'Tom' in line).map(lambda line:line.split(','))
Tom.collect()

  1. Tom选修了几门课?每门课多少分?map(),lookup() list

lines.map(lambda line:line.split(',')).map(lambda line:(line[0],line[2])).lookup('Tom')

  1. Tom的成绩按分数大小排序。filter(), map(), sortBy()

Tom.sortBy(lambda word:word[2],False).collect()

  1. Tom的平均分。map(),lookup(),mean()
from numpy import mean
tomList=lines.map(lambda line:line.split(',')).map(lambda line:(line[0],line[2])).lookup('Tom')
mean([int(x) for x in tomList])

  1. 生成(课程,分数)RDD,观察keys(),values()
course=lines.map(lambda x:x.split(',')).map(lambda x:(x[1],x[2]))
course.keys().take(10)
course.values().take(10)

  1. 每个分数+5分。mapValues(func)

course.map(lambda x:(x[0],int(x[1]))).mapValues(lambda x:x+5).take(10)

  1. 求每门课的选修人数及所有人的总分。combineByKey()
courseC=course.combineByKey(lambda v: (int(v),1),lambda c,v:(c[0]+int(v),c[1]+1),lambda c1,c2:(c1[0]+c2[0],c1[1]+c2[1]))
courseC.collect()

  1. 求每门课的选修人数及平均分,精确到2位小数。map(),round()

courseC.map(lambda x:(x[0],x[1][1],round(x[1][0]/x[1][1]))).collect()

  1. 求每门课的选修人数及平均分。用reduceByKey()实现,并比较与combineByKey()的异同。

lines.map(lambda line:line.split(',')).map(lambda x:(x[1],(int(x[2]),1))).reduceByKey(lambda a,b:(a[0]+b[0],a[1]+b[1])).map(lambda y:(y[0],y[1][1],round(y[1][0]/y[1][1]))).collect()

posted @ 2021-04-17 19:38  会喷水的海参  阅读(98)  评论(0编辑  收藏  举报