当前位置:网站首页>Spark 算子之sortBy使用
Spark 算子之sortBy使用
2022-04-23 15:45:00 【逆风飞翔的小叔】
前言
sortBy,顾名思义排序的意思,在Spark 中,使用sortBy可以对一组待处理的数据进行排序,这组数据不限于数字,也可以是元组等其他类型;
sortBy
函数签名
def sortBy[K](f: (T) => K , ascending: Boolean = true , numPartitions: Int = this.partitions.length)(implicit ord: Ordering[K], ctag: ClassTag[K]): RDD[T]
函数说明
该操作用于排序数据。在排序之前,可以将数据通过 f 函数进行处理,之后按照 f 函数处理的结果进行排序,默认为升序排列。排序后新产生的 RDD 的分区数与原 RDD 的分区数一致。中间存在 shuffle 的过程;
案例演示
下面将一组集合中的数据排序后,保存到本地文件目录中
import org.apache.spark.{SparkConf, SparkContext}
object SortBy_Test {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
val sc = new SparkContext(sparkConf)
val rdd = sc.makeRDD(List(1,2,3,4,5,6,7,9), 2)
rdd.sortBy(num => num)
rdd.saveAsTextFile("E:\\output")
sc.stop()
}
}
运行上面的代码,可以看到在本地目录下生成了两个文件
分别打开2个文件,可以发现,数据在两个不同的文件中各自进行了排序
将集合中的tuple数据按照key进行排序输出
import org.apache.spark.{SparkConf, SparkContext}
object SortBy_Test {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
val sc = new SparkContext(sparkConf)
var rddStr = sc.makeRDD(List(
("a",3),("d",2),("e",7)
),2)
rddStr.sortBy(t => t._1)
rddStr.collect().foreach(println)
sc.stop()
}
}
运行上面的代码,观察控制台输出结果
版权声明
本文为[逆风飞翔的小叔]所创,转载请带上原文链接,感谢
https://blog.csdn.net/congge_study/article/details/124360097
边栏推荐
猜你喜欢
[leetcode daily question] install fence
CVPR 2022 quality paper sharing
Application of Bloom filter in 100 million flow e-commerce system
Basic concepts of website construction and management
山寨版归并【上】
Timing model: gated cyclic unit network (Gru)
Special analysis of China's digital technology in 2022
大型互联网为什么禁止ip直连
为啥禁用外键约束
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
随机推荐
How do you think the fund is REITs? Is it safe to buy the fund through the bank
Best practices of Apache APIs IX high availability configuration center based on tidb
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
Code live collection ▏ software test report template Fan Wen is here
怎么看基金是不是reits,通过银行购买基金安全吗
王启亨谈Web3.0与价值互联网“通证交换”
Upgrade MySQL 5.1 to 5.69
时序模型:门控循环单元网络(GRU)
gps北斗高精度卫星时间同步系统应用案例
网站压测工具Apache-ab,webbench,Apache-Jemeter
Mumu, go all the way
移动app测试如何进行?
计算某字符出现次数
The length of the last word of the string
shell脚本中的DATE日期计算
php类与对象
PHP 的运算符
Multitimer V2 reconstruction version | an infinitely scalable software timer
Modèle de Cluster MySQL et scénario d'application