首页 技术 正文
技术 2022年11月12日
0 收藏 956 点赞 3,859 浏览 1011 个字

注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6815390070254600712/

承接上一个文档《Spark本地环境实现wordCount单词计数

进一步延伸,做一个词频前十的统计练习

逻辑:在reduceByKey的基础上,首先要根据key对应的value值进行排序(降序排序),取前10个的结果就是Top10

val reduceByKeyRDD = sc.textFile("file:///opt/bigdata/spark/README.md").flatMap(_.split(" ")).filter(_.nonEmpty).map((_,1)).reduceByKey(_+_)

reduceByKeyRDD.sortBy(t => t._2,ascending=false)

reduceByKeyRDD.sortBy(t => t._2,ascending=false).take(10)

sortBy函数:第一个匿名函数表示按照元组的第二个元素进行排序,ascending=false表示按照降序排序,如果不指定这个参数,默认是升序的排序

reduceByKeyRDD.sortBy(t => t._2 * -1).take(10)

也实现了降序排列,提取TOP10

下面这个方法也可以

reduceByKeyRDD.map(t => t.swap).sortByKey(ascending=false).map(t => t.swap).take(10)

分解看下:

reduceByKeyRDD.map(t => t.swap).sortByKey(ascending=false).
t.swap :("the",22) --> (22,"the") --> ("the",22)

reduceByKeyRDD.map(t => t.swap).sortByKey(ascending=false).map(t => t.swap).take(10)

下面这个性能会更好:

reduceByKeyRDD.map(t => t.swap).sortByKey(ascending=false).take(10).map(t => t.swap)

用top(10)代替sortByKey(ascending=false).take(10)这一部分

reduceByKeyRDD.map(t => t.swap).top(10).map(t => t.swap)

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848