当前位置:网站首页>Spark 算子之partitionBy
Spark 算子之partitionBy
2022-04-23 15:45:00 【逆风飞翔的小叔】
前言
在之前的学习中,我们使用groupBy可以对数据按照指定的key的规则进行分组,设想这样一种场景,如果要对 tuple类型的数据,即key/value类型的数据进行分组该怎么做呢?针对这种的话Spark提供了partitionBy算子解决;
partitionBy
函数签名
def partitionBy( partitioner: Partitioner ): RDD[(K, V)]
函数说明
将数据按照指定 Partitioner 重新进行分区。 Spark 默认的分区器是 HashPartitioner
案例展示
将一组数据通过partitionBy之后存储到多个分区文件中
import org.apache.spark.rdd.RDD
import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}
object PartionBy_Test {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
val sc = new SparkContext(sparkConf)
// TODO 算子 - (Key - Value类型)
val rdd = sc.makeRDD(List(1, 2, 3, 4), 2)
val mapRDD: RDD[(Int, Int)] = rdd.map((_, 1))
// partitionBy根据指定的分区规则对数据进行重分区
val newRDD = mapRDD.partitionBy(new HashPartitioner(2)).saveAsTextFile("E:\\output")
sc.stop()
}
}
运行上面的代码,执行完成后,观察本地的目录下,可以看到4条数据被分不到不同的分区文件中
版权声明
本文为[逆风飞翔的小叔]所创,转载请带上原文链接,感谢
https://blog.csdn.net/congge_study/article/details/124362294
边栏推荐
- Upgrade MySQL 5.1 to 5.67
- 自动化测试框架常见类型▏自动化测试就交给软件测评机构
- C language --- string + memory function
- 编译,连接 -- 笔记
- 基础贪心总结
- PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
- The length of the last word of the string
- Cap theorem
- 小程序知识点积累
- Redis主从复制过程
猜你喜欢
cadence SPB17.4 - Active Class and Subclass
大厂技术实现 | 行业解决方案系列教程
负载均衡器
Neodynamic Barcode Professional for WPF V11.0
山寨版归并【上】
JVM-第2章-类加载子系统(Class Loader Subsystem)
Codejock Suite Pro v20. three
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
Why disable foreign key constraints
网站压测工具Apache-ab,webbench,Apache-Jemeter
随机推荐
大型互联网为什么禁止ip直连
For examination
Go语言切片,范围,集合
One brush 313 sword finger offer 06 Print linked list from end to end (E)
KNN, kmeans and GMM
Upgrade MySQL 5.1 to 5.611
JVM-第2章-类加载子系统(Class Loader Subsystem)
Treatment of idempotency
C#,贝尔数(Bell Number)的计算方法与源程序
基础贪心总结
Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
Extract non duplicate integers
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
Cookie&Session
通过 PDO ODBC 将 PHP 连接到 MSSQL
Basic concepts of website construction and management
Redis master-slave replication process
The El tree implementation only displays a certain level of check boxes and selects radio
shell_2
Recommended search common evaluation indicators