Spark RDD基本轉換

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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章