求包养QAQ

RDD操作综合实例

一、词频统计

A. 分步骤实现

  1. 准备文件

    1. 下载小说或长篇新闻稿

      image-20220601165605674

    2. 上传到hdfs上

      hdfs dfs -put ./data/my.txt ./
      hdfs dfs -ls ./
      

      image-20220601170206459

  2. 读文件创建RDD

    lines=sc.textFile("hdfs://localhost:9000/user/hjq/my.txt")
    lines
    

    image-20220601170420380

  3. 分词

    words = lines.flatMap(lambda a:a.split())
    words.collect()
    

    image-20220601170439841

  4. (1)排除大小写lower(),map()

    words2 = words.map(lambda a:a.lower())
    words2.collect()
    

    image-20220601170554274

    (2)标点符号re.split(pattern,str),flatMap()

    导入re,通过re.split('\W+',a)进行对标点符号进行

注:\W匹配任何非单词字符,(\W+)匹配任何非单词字符并返回去掉的值

import re
words3 = words2.flatMap(lambda a:re.split('\W+', a))
words3.collect()

image-20220601170636929

清除多出来的空白数据

words4 = words3.flatMap(lambda a:a.split())
words4.collect()

image-20220601170701566

(3)停用词,可网盘下载stopwords.txt,filter()

hdfs dfs -put stopwords.txt ./
hdfs dfs -ls ./

image-20220601170809254

将停用词文件分词储存到变量stopword

stopword = sc.textFile('stopwords.txt').flatMap(lambda a:a.split()).collect()
stopword

image-20220601170854806

筛选出不在停用表中的词

words5 = words4.filter(lambda a:a not in stopword)
words5.collect()

image-20220601170921331

筛选前后对比

len(words4.collect())
len(words5.collect())

image-20220601171011057

(4)排除长度小于2的词filter()

words6 = words5.filter(lambda a:len(a)>2)
words6.collect()

image-20220601171034915

  1. 统计词频

    (1)先将单词映射成键值对

    wordkv = words6.map(lambda a:(a,1))
    wordkv.collect()
    

    image-20220601171052377

    (2)映射键值对再将key相同的values合并起来,完成词频的统计

     wordkv = wordkv.reduceByKey(lambda a,b:a+b)
     wordkv.collect()
    

image-20220601171115128

  1. 按词频降序排序

    wordsort = wordkv.sortBy(lambda x:x[1], False)
    wordsort.collect()
    

    image-20220601171137512

  2. 输出到文件

    wordsort.saveAsTextFile('rdd_5')
    wordsort.saveAsTextFile('file:///home/hjq/rdd_5')
    

    image-20220601171250910

    image-20220601171332019

  3. 查看结果

    查看文件前10条结果

    image-20220601171630288

B. 一句话实现:文件入文件出

sc.textFile("file:///home/hjq/data/my.txt").flatMap(lambda a:a.split()).flatMap(lambda a:re.split('\W+', a)).filter(lambda a:len(a)>2).map(lambda a:(a,1)).reduceByKey(lambda a,b:a+b).sortBy(lambda x:x[1],False).saveAsTextFile('rdd_onecode')

image-20220602160324911

查看文件(由于没有去除停用表内的词,所以结果可能会与前面不同)

image-20220602160432030

C. 和作业2的“二、Python编程练习:英文文本的词频统计 ”进行比较,理解Spark编程的特点。

在Spark中,RDD允许用户显式地将工作集缓存在内存中,后续能够重用工作集,这极大地提升了速度。

其中,Spark提供的主要抽象是弹性分布式数据集(RDD),通常RDD很大,会被分成很多个分区,分别保存在不同的节点上。分区可以增加并行度,减少通信开销。

RDD通过打开HDFS(或其他hadoop支持的文件系统)上的一个文件、在驱动程序中打开一个已有的Scala集合或由其他RDD转换操作得到。

用户可以要求Spark将RDD持久化到内存中,这样就可以有效地在并行操作中复用。另外,在节点发生错误时RDD可以自动恢复。

二、求Top值

网盘下载payment.txt文件,通过RDD操作实现选出最大支付额的用户。

将文件上传到hdfs中

image-20220602160611908

1.丢弃不合规范的行:

import re
account = sc.textFile('payment.txt').flatMap(lambda x:x.split()).flatMap(lambda x:re.split('\W+',x)).flatMap(lambda a:a.split())
account.collect()

image-20220602160641017

2.按支付金额排序

  • 将金额映射成KV值相同的键值对,这里映射value注意要转为int类型
accountkv = account.map(lambda a:(a,int(a)))
accountkv.collect()

image-20220602160708682

  • 对键值对进行累加统计和排序
accountsort = accountkv.reduceByKey(lambda a,b:a+b).sortBy(lambda x:x[1], False)
accountsort.collect()

image-20220602160728479

3.取出Top3

输出文件并查看结果

image-20220602160824315

image-20220602160920512

posted @ 2022-04-07 21:26  Baby_Monster  阅读(92)  评论(0)    收藏  举报