spark編程模型(十六)之RDD鍵值轉換操作(Transformation Operation)——leftOuterJoin、rightOuterJoin、subtractByKey

leftOuterJoin

  • def leftOuterJoin[W](other: RDD[(K, W)]): RDD[(K, (V, Option[W]))]
  • def leftOuterJoin[W](other: RDD[(K, W)], numPartitions: Int): RDD[(K, (V, Option[W]))]
  • def leftOuterJoin[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (V, Option[W]))]
  • leftOuterJoin類似於SQL中的左外關聯left outer join,返回結果以前面的RDD爲主,關聯不上的記錄爲空。只能用於兩個RDD之間的關聯,如果要多個RDD關聯,多關聯幾次即可
  • 參數numPartitions用於指定結果的分區數
  • 參數partitioner用於指定分區函數

    var rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
    var rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)
    
    scala> rdd1.leftOuterJoin(rdd2).collect
    res11: Array[(String, (String, Option[String]))] = Array((B,(2,None)), (A,(1,Some(a))), (C,(3,Some(c))))
    

rightOuterJoin

  • def rightOuterJoin[W](other: RDD[(K, W)]): RDD[(K, (Option[V], W))]
  • def rightOuterJoin[W](other: RDD[(K, W)], numPartitions: Int): RDD[(K, (Option[V], W))]
  • def rightOuterJoin[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (Option[V], W))]
  • rightOuterJoin類似於SQL中的有外關聯right outer join,返回結果以參數中的RDD爲主,關聯不上的記錄爲空。只能用於兩個RDD之間的關聯,如果要多個RDD關聯,多關聯幾次即可
  • 參數numPartitions用於指定結果的分區數
  • 參數partitioner用於指定分區函數

    var rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
    var rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)
    
    scala> rdd1.rightOuterJoin(rdd2).collect
    res12: Array[(String, (Option[String], String))] = Array((D,(None,d)), (A,(Some(1),a)), (C,(Some(3),c)))
    

subtractByKey

  • def subtractByKey[W](other: RDD[(K, W)])(implicit arg0: ClassTag[W]): RDD[(K, V)]
  • def subtractByKey[W](other: RDD[(K, W)], numPartitions: Int)(implicit arg0: ClassTag[W]): RDD[(K, V)]
  • def subtractByKey[W](other: RDD[(K, W)], p: Partitioner)(implicit arg0: ClassTag[W]): RDD[(K, V)]
  • subtractByKey和基本轉換操作中的subtract類似,只不過這裏是針對K的,返回在主RDD中出現,並且不在otherRDD中出現的元素
  • 參數numPartitions用於指定結果的分區數
  • 參數partitioner用於指定分區函數

    var rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
    var rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)
    
    scala> rdd1.subtractByKey(rdd2).collect
    res13: Array[(String, String)] = Array((B,2))
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章