5.RDD操作综合实例

一、词频统计

A. 分步骤实现 

    1. 准备文件
      1. 下载小说或长篇新闻稿
      2. 上传到hdfs上
    2. 读文件创建RDD
    3. 分词

       

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

            

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

     

      

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

     

      

      长度小于2的词filter()

      

 

 

      5.统计词频

      

 

       

 

 

      6.按词频排序

     

 

 

      7.输出到文件

      

 

 

      8.查看结果

 

 

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

      lines = sc.textFile("hdfs://localhost:9000/user/hadoop/test.txt").flatMap(lambda line: line.split()).flatMap(lambda line: re.split(r'\W', line)).flatMap(lambda line: line.split()).map(lambda word: word.lower()).filter(lambda x: x not in stopwords).filter(lambda x: len(x) > 2).map(lambda a: (a, 1)).reduceByKey(lambda a, b: a + b).sortBy(lambda x: x[1], False)

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

       Spark运行速度快、易用性好、通用性强和随处运行。

 

      二、求Top值

    

 

posted on 2022-04-07 20:41  Luirc  阅读(22)  评论(0编辑  收藏  举报