05 RDD编程

一、词频统计:

1.读文本文件生成RDD lines

代码:lines = sc.textFile('file:///home/hadoop/word.txt')

2.将一行一行的文本分割成单词 words flatmap()

代码:words=lines.flatMap(lambda line:line.split())

   words.collect()

3.全部转换为小写 lower()

代码:#lines.flatMap(lambda line:line.lower().split()).collect()

   words=lines.flatMap(lambda line:line.lower().split())

   words.collect()

4.去掉长度小于3的单词 filter()

代码:words.filter(lambda word : len(word)>3).collect()

5.去掉停用词

准备停用词文本

代码:

1.准备停用词文本:

lines = sc.textFile('file:///home/hadoop/stopwords.txt')
stop = lines.flatMap(lambda line : line.split()).collect()

 2.去除停用词:

words=lines.flatMap(lambda line:line.lower().split()).filter(lambda word : word not in stop)

words.collect()

6.转换成键值对 map()

代码:

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

7.统计词频 reduceByKey()

代码:

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

8.按字母顺序排序 sortBy(f)

代码:

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

9.按词频排序 sortByKey()

代码:

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

10.结果文件保存 saveAsTextFile(out_url)

代码:

lines = sc.textFile('file:///home/hadoop/chapter4-data01.txt')

course_rev = 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]))

course_rev.saveAsTextFile("file:///home/hadoop/out_url")

11. 词频结果可视化charts.WordCloud()

 问题描述:1. pip在Ubuntu系统中不自带;

      2. python自带版本2.7.1

      3. python最新版本与pip不匹配(可能出现)

问题解决: 1. sudo apt-get install python3-pip

      2. python3.6 -m pip install --upgrade pip

       sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1

       sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2

       sudo update-alternatives --config python

12. 比较不同框架下(Python、MapReduce、Hive和Spark),实现词频统计思想与技术上的不同,各有什么优缺点.

二、学生课程分数案例

    • 总共有多少学生?map(), distinct(), count()
      • 代码:lines.map(lambda line : line.split(',')[0]).distinct().count()
    • 开设了多少门课程?
      • 代码:lines.map(lambda line : line.split(',')[1]).distinct().count()
    • 每个学生选修了多少门课?map(), countByKey()
      • 代码:lines.map(lambda line : line.split(',')).map(lambda line:(line[0],line[2])).countByKey()
    • 每门课程有多少个学生选?map(), countByValue()
      • 代码:lines.map(lambda line : line.split(',')).map(lambda line : (line[0])).countByValue()
    • Roy选修了几门课?每门课多少分?filter(), map() RDD
      • lines.filter(lambda line:"Roy" in line).map(lambda line:line.split(',')).collect()
    • Roy选修了几门课?每门课多少分?map(),lookup()  list
      • lines.map(lambda line:line.split(',')).map(lambda line:(line[0],line[1],line[2])).lookup("Roy")

    • Roy的成绩按分数大小排序。filter(), map(), sortBy()
      • lines.filter(lambda line:"Roy" in line).map(lambda line:line.split(',')).sortBy(lambda line:(line[2])).collect()
    • Roy的平均分。map(),lookup(),mean()
      • 代码:import numpy as np
      • meanlist=lines.map(lambda line:line.split(',')).map(lambda line:(line[0],line[2])).lookup("Roy")
      • np.mean([int(x) for x in meanlist])
    • 生成(课程,分数)RDD,观察keys(),values()
      • 代码:lines = sc.textFile('file:///home/hadoop/chapter4-data01.txt')
      • words = lines.map(lambda x:x.split(',')).map(lambda x:(x[1],x[2]))
      • words.keys().take(5)
      • words.values().take(5)
    • 每个分数+5分。mapValues(func)
      • 代码:words = words.map(lambda x:(x[0],int(x[1])))
      • words.mapValues(lambda x:x+1).foreach(print)
    • 求每门课的选修人数及所有人的总分。combineByKey()
      • 代码:course = words.combineByKey(lambda v:(v,1),lambda c,v:(c[0]+v,c[1]+1),lambda c1,c2:(c1[0]+c2[0],c1[1]+c2[1]))
    • 求每门课的选修人数及平均分,精确到2位小数。map(),round()
      • 代码:course_rev = course.map(lambda x:(x[0],x[1][1],round(x[1][0]/x[1][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])).foreach(print)
    • 结果可视化。charts,Bar()
posted @ 2021-04-16 17:10  starrysky~ocean  阅读(145)  评论(0编辑  收藏  举报