RDD操作实现

  • 1.文本文件:
    • 方式一:没用正则处理的
    • 方式二:添加正则表达式
    • 方式三:利用sortBy()进行排序
  • Spark sql实现
    • 导入隐式转换
    • createOrReplaceTempView使用

1.文本文件:

Preface
“The Forsyte Saga” was the title originally destined for that part of it which is called “The Man of Property”;
and to adopt it for the collected chronicles of the Forsyte family has indulged the Forsytean tenacity that is in all of us.
The word Saga might be objected to on the ground that it connotes the heroic and that there is little heroism in these pages.But it is used with a suitable irony; and, after all, this long tale, though it may deal with folk in frock coats, furbelows

方式一:没用正则处理的

val file = spark.sparkContext.textFile("file:///F:\\dataset\\The_Man_of_Property.txt")file.flatMap(line=>line.split(" ")).map((_,1)).reduceByKey(_+_).sortBy(_._2,ascending = false).take(10).foreach(println(_))

方式二:添加正则表达式

val p = "[0-9a-zA-Z]+".rfile.flatMap(line => line.split(" ")).map(x=>(p.findAllIn(x).mkString(""),1)).reduceByKey(_+_).take(10).foreach(println)结果:
(welshed,1)
(mattered,1)
(someone,10)
(disregarded,2)
(jowl,2)
(bone,1)
(House,6)

方式三:利用sortBy()进行排序

sortBy(_._2,ascending = false)
默认升序
ascending = false 代表:降序val p = "[0-9a-zA-Z]+".rfile.flatMap(line => line.split(" ")).map(x=>(p.findAllIn(x).mkString(""),1)).reduceByKey(_+_).sortBy(_._2,ascending = false).take(10).foreach(println)
结果:
(the,5168)
(of,3425)
(to,2810)
(and,2686)
(a,2564)

Spark sql实现

导入隐式转换

 import spark.implicits._
//hdfs上文件val file = spark.sparkContext.textFile("/data/The_Man_of_Property.txt")val p = "[0-9a-zA-Z]+".rval dataFrame = file.flatMap(line=>line.split(" ")).map(x=>(p.findAllIn(x).mkString(""),1)).reduceByKey(_+_).toDF("word","count")dataFrame.createOrReplaceTempView("Property")dataFrame.show(5)
+-----------+-----+
|       word|count|
+-----------+-----+
|    welshed|    1|
|   mattered|    1|
|    someone|   10|
|disregarded|    2|
|       jowl|    2|
+-----------+-----+
only showing top 5 rows

createOrReplaceTempView使用

createOrReplaceTempView:创建临时视图,此视图的生命周期与用于创建此数据集的[SparkSession]相关联。
dataFrame.createOrReplaceTempView("Property")//指定列名
scala> val res = spark.sql("select * from Property ")
21/01/12 20:25:20 WARN metastore.ObjectStore: Failed to get database global_temp, returning NoSuchObjectException
res: org.apache.spark.sql.DataFrame = [word: string, count: int]scala> res.show(5)
+-----------+-----+
|       word|count|
+-----------+-----+
|    welshed|    1|
|   mattered|    1|
|    someone|   10|
|disregarded|    2|
|       jowl|    2|
+-----------+-----+
only showing top 5 rows

Spark实现WordCount案例相关推荐

  1. Spark之WordCount案例

    创建project,创建module,引入scala,添加spark依赖 <?xml version="1.0" encoding="UTF-8"?> ...

  2. MapReduce和sparks运行wordcount案例过程分析

    MapReduce执行wordcount案例分析 1.先将磁盘中的文件读入到内存,按行读取,如图所示 2.将文件分割成每个一行一行数据之后,MapReduce框架会自动将我们的一行一行数据转化为< ...

  3. Spark综合小案例之莎士比亚诗文集词频统计

    教程目录 0x00 教程内容 0x01 数据准备 1. 数据获取 2. 数据内容 0x02 代码实现 1. 启动spark-shell 2. 测试代码 0x03 校验结果 1. 查看是否有统计结果 0 ...

  4. Spark快速上手-WordCount案例

    在此之前,我已经用MapReduce 框架实现了WordCount案例,接下来,我开始学习数据处理的另外一个非常重要的方法:Spark.首先,使用WordCount案例实现Spark快速上手. 创建M ...

  5. Spark WordCount 案例

    文章目录 Spark WordCount 案例 1.程序连接 Spark 2.WordCount 案例示例 3.复杂版 WordCount 4.Spark 框架Wordcount Spark Word ...

  6. Scala语言实现WordCount案例以及几个高级函数的使用总结

    实现案例前需要熟悉scala中集合的几个高级函数: 映射:集合.map() 即拿到集合中元素做某些处理,返回当前集合的类型. 扁平化:集合.flatten() 就是提取外层集合中的内层集合中的元素,打 ...

  7. WordCount案例

    WordCount案例 需求 1. 需求说明 2. 文件 案例分析 1.需求分析 2.输入数据 3.期望输出数据 4.Mapper类 5. Reducer类 6. Driver类 代码实现 1. 编写 ...

  8. MapReduce流程(WordCount案例实现)

    文章目录 1 MapReduce概述 设计构思 实例进程 实例进程分类 完整执行过程 总结 2 MapReduce编程规范 Map阶段2个步骤 Shuffle阶段4个步骤 Reduce阶段2个步骤 3 ...

  9. Spark action算子案例

    在上篇文章中,我们对Spark中几种常用的transformation算子通过Java和Scala两种代码分别进行了案例演示,Spark transformation算子案例  而在本文中,我们将继续 ...

最新文章

  1. 某程序员对比美团和阿里的卷文化区别:美团重过程,死抠没用细节;阿里更自由,注重结果!...
  2. 让IE兼容background-size的方法_background-size ie下使用
  3. flask高级编程 LocalStack 线程隔离
  4. Face-landmarks-detection-benchmark 人脸特征定位网站汇总
  5. 简述python的编程规范_python编程规范
  6. ActiveMq笔记3-AMQ高可用性理论
  7. linux如何重启syslog服务,Linux syslog服务
  8. 100行python代码能做什么,100行python代码实现跳一跳辅助程序
  9. html网页设计课程设计总结,网页制作课程设计报告总结.doc
  10. 解决微信开发者工具无法打开的问题
  11. 学习利器,借助Tampermonkey写一个B站视频加速器脚本
  12. 【Git】工作区、暂存区与版本库
  13. 谷歌浏览器如何给长网页截图?
  14. 【射频知识】吸波材料
  15. BLDC无刷电机驱动板,foc驱动板,有霍尔接口,反电动势接口,三相电流采集接口
  16. 百鸡问题用计算机思维,大力出奇迹:当古代数学难题遇到计算机
  17. python中θ符号怎么打出来_各种符号在键盘上怎么打出来?
  18. CentOS通过Samba访问NAS共享目录
  19. 华为S5300系列交换机V100R006SPH019升级补丁
  20. 【入坑Java第二天】

热门文章

  1. 像excel一样规律填充(二)
  2. Thinkbayes_Chapter5
  3. redis 数据类型详解 以及 redis适用场景场合
  4. 五年 Web 开发者 star 的 github 整理说明
  5. linux源代码剖析之include-asm
  6. Oracle 服务作用
  7. 北理计算机语言智能与社会计算,北京理工大学校长张军描绘智慧社会:人在思、云在算、端在造...
  8. 编程语言的语法与语义
  9. html5制作线路图,HTML5画电路图
  10. oracle机票,全球机票分销系统