Spark算子:RDD键值转换操作(4)–cogroup、join

关键字:Spark算子、Spark RDD键值转换、cogroup、join

cogroup

##参数为1个RDD

def cogroup[W](other: RDD[(K, W)]): RDD[(K, (Iterable[V], Iterable[W]))]

def cogroup[W](other: RDD[(K, W)], numPartitions: Int): RDD[(K, (Iterable[V], Iterable[W]))]

def cogroup[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (Iterable[V], Iterable[W]))]

 

##参数为2个RDD

def cogroup[W1, W2](other1: RDD[(K, W1)], other2: RDD[(K, W2)]): RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2]))]

def cogroup[W1, W2](other1: RDD[(K, W1)], other2: RDD[(K, W2)], numPartitions: Int): RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2]))]

def cogroup[W1, W2](other1: RDD[(K, W1)], other2: RDD[(K, W2)], partitioner: Partitioner): RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2]))]

 

##参数为3个RDD

def cogroup[W1, W2, W3](other1: RDD[(K, W1)], other2: RDD[(K, W2)], other3: RDD[(K, W3)]): RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2], Iterable[W3]))]

def cogroup[W1, W2, W3](other1: RDD[(K, W1)], other2: RDD[(K, W2)], other3: RDD[(K, W3)], numPartitions: Int): RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2], Iterable[W3]))]

def cogroup[W1, W2, W3](other1: RDD[(K, W1)], other2: RDD[(K, W2)], other3: RDD[(K, W3)], partitioner: Partitioner): RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2], Iterable[W3]))]

 

cogroup相当于SQL中的全外关联full outer join,返回左右RDD中的记录,关联不上的为空。

参数numPartitions用于指定结果的分区数。

参数partitioner用于指定分区函数。

##参数为1个RDD的例子

 
 
  1. var rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
  2. var rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)
  3.  
  4. scala> var rdd3 = rdd1.cogroup(rdd2)
  5. rdd3: org.apache.spark.rdd.RDD[(String, (Iterable[String], Iterable[String]))] = MapPartitionsRDD[12] at cogroup at :25
  6.  
  7. scala> rdd3.partitions.size
  8. res3: Int = 2
  9.  
  10. scala> rdd3.collect
  11. res1: Array[(String, (Iterable[String], Iterable[String]))] = Array(
  12. (B,(CompactBuffer(2),CompactBuffer())),
  13. (D,(CompactBuffer(),CompactBuffer(d))),
  14. (A,(CompactBuffer(1),CompactBuffer(a))),
  15. (C,(CompactBuffer(3),CompactBuffer(c)))
  16. )
  17.  
  18.  
  19. scala> var rdd4 = rdd1.cogroup(rdd2,3)
  20. rdd4: org.apache.spark.rdd.RDD[(String, (Iterable[String], Iterable[String]))] = MapPartitionsRDD[14] at cogroup at :25
  21.  
  22. scala> rdd4.partitions.size
  23. res5: Int = 3
  24.  
  25. scala> rdd4.collect
  26. res6: Array[(String, (Iterable[String], Iterable[String]))] = Array(
  27. (B,(CompactBuffer(2),CompactBuffer())),
  28. (C,(CompactBuffer(3),CompactBuffer(c))),
  29. (A,(CompactBuffer(1),CompactBuffer(a))),
  30. (D,(CompactBuffer(),CompactBuffer(d))))
  31.  

##参数为2个RDD的例子

 
 
  1. var rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
  2. var rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)
  3. var rdd3 = sc.makeRDD(Array(("A","A"),("E","E")),2)
  4.  
  5. scala> var rdd4 = rdd1.cogroup(rdd2,rdd3)
  6. rdd4: org.apache.spark.rdd.RDD[(String, (Iterable[String], Iterable[String], Iterable[String]))] =
  7. MapPartitionsRDD[17] at cogroup at :27
  8.  
  9. scala> rdd4.partitions.size
  10. res7: Int = 2
  11.  
  12. scala> rdd4.collect
  13. res9: Array[(String, (Iterable[String], Iterable[String], Iterable[String]))] = Array(
  14. (B,(CompactBuffer(2),CompactBuffer(),CompactBuffer())),
  15. (D,(CompactBuffer(),CompactBuffer(d),CompactBuffer())),
  16. (A,(CompactBuffer(1),CompactBuffer(a),CompactBuffer(A))),
  17. (C,(CompactBuffer(3),CompactBuffer(c),CompactBuffer())),
  18. (E,(CompactBuffer(),CompactBuffer(),CompactBuffer(E))))
  19.  
  20.  

##参数为3个RDD示例略,同上。

join

def join[W](other: RDD[(K, W)]): RDD[(K, (V, W))]

def join[W](other: RDD[(K, W)], numPartitions: Int): RDD[(K, (V, W))]

def join[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (V, W))]

 

join相当于SQL中的内关联join,只返回两个RDD根据K可以关联上的结果,join只能用于两个RDD之间的关联,如果要多个RDD关联,多关联几次即可。

参数numPartitions用于指定结果的分区数

参数partitioner用于指定分区函数

 
 
  1. var rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
  2. var rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)
  3.  
  4. scala> rdd1.join(rdd2).collect
  5. res10: Array[(String, (String, String))] = Array((A,(1,a)), (C,(3,c)))
  6.  

更多关于Spark算子的介绍,可参考spark算子系列文章:

http://blog.csdn.net/ljp812184246/article/details/53895299

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值