Spark RDD基本转换

本文深入解析了Spark中RDD的三种关键操作:Union、Intersection和Subtract的功能与用法。通过示例代码展示了如何使用这些操作来处理数据集,包括合并、求交集和差集的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

union、intersection、subtract

union

def union(other: RDD[T]): RDD[T]

该函数比较简单,就是将两个RDD进行合并,不去重

scala> var rdd1 = sc.makeRDD(Seq(1,2,2,3))
rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at makeRDD at <console>:27
scala> var rdd2 = sc.makeRDD(3 to 4)
rdd2: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[1] at makeRDD at <console>:27
scala> rdd1.union(rdd2).collect
res7: Array[Int] = Array(1, 2, 2, 3, 3, 4)

intersection

def intersection(other: RDD[T]): RDD[T]
def intersection(other: RDD[T], numPartitions: Int): RDD[T]
def intersection(other: RDD[T], partitioner: Partitioner)(implicit ord: Ordering[T] = null): RDD[T]

该函数返回两个RDD的交集,并且去重
参数numPartitions指定返回的RDD的分区数。
参数partitioner用于指定分区函数

scala> rdd1.intersection(rdd2).collect
res8: Array[Int] = Array(3)

subtract

def subtract(other: RDD[T]): RDD[T]
def subtract(other: RDD[T], numPartitions: Int): RDD[T]
def subtract(other: RDD[T], partitioner: Partitioner)(implicit ord: Ordering[T] = null): RDD[T]

该函数类似于intersection,但返回在RDD中出现,并且不在otherRDD中出现的元素,不去重
参数含义同intersection

scala> rdd1.subtract(rdd2).collect
res9: Array[Int] = Array(1, 2, 2)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不加班程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值