微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spark算子:RDD基本转换transform操作–union、intersection、subtract

Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 2.4.5.online-JD2.4.5.16-202012212053
      /_/

Using Scala version 2.11.12 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121)
Type in expressions to have them evaluated.
Type :help for more information.

union

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

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

scala> var rdd1 = sc.makeRDD(1 to 2,1)
rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[45] at makeRDD at :21
 
scala> rdd1.collect
res42: Array[Int] = Array(1, 2)
 
scala> var rdd2 = sc.makeRDD(2 to 3,1)
rdd2: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[46] at makeRDD at :21
 
scala> rdd2.collect
res43: Array[Int] = Array(2, 3)
 
scala> rdd1.union(rdd2).collect
res44: Array[Int] = Array(1, 2, 2, 3)

 

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>  var rdd1 = sc.makeRDD(Seq(1,2,2,3))
rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[2] at makeRDD at <console>:24

scala>  var rdd2 = sc.makeRDD(Seq(2,3,3,4,5))
rdd2: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[3] at makeRDD at <console>:24

scala> var rdd3 = rdd1.intersection(rdd2).collect;

rdd3: Array[Int] = Array(2, 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> var rdd1 = sc.makeRDD(Seq(1,1,1,2,2,3,6,6,7))
rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[16] at makeRDD at <console>:24

scala> var rdd3 = rdd1.subtract(rdd2).collect;

rdd3: Array[Int] = Array(1, 1, 1, 6, 6, 7)

参考http://lxw1234.com/archives/2015/07/345.htm

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐