Apache Spark中join和cogroup之间的区别是什么

时间:2017-05-14 05:08:16

标签: scala apache-spark

Apache Spark中的join和cogroup有什么区别?每种方法的用例是什么?

1 个答案:

答案 0 :(得分:46)

让我帮你澄清一下,两者都很常用,很重要!

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

这是prototype加入,请仔细查看。例如,

val rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
val rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)

scala> rdd1.join(rdd2).collect
res0: Array[(String, (String, String))] = Array((A,(1,a)), (C,(3,c)))

最终结果中出现的所有键对于 rdd1和rdd2是通用的。这类似于relation database operation INNER JOIN

但是cogroup是不同的

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

作为一个键至少出现在两个rdds中的任何一个,它将出现在最终结果中,让我澄清一下:

val rdd1 = sc.makeRDD(Array(("A","1"),("B","2"),("C","3")),2)
val rdd2 = sc.makeRDD(Array(("A","a"),("C","c"),("D","d")),2)

scala> var rdd3 = rdd1.cogroup(rdd2).collect
res0: Array[(String, (Iterable[String], Iterable[String]))] = Array(
(B,(CompactBuffer(2),CompactBuffer())), 
(D,(CompactBuffer(),CompactBuffer(d))), 
(A,(CompactBuffer(1),CompactBuffer(a))), 
(C,(CompactBuffer(3),CompactBuffer(c)))
)

similarrelation database operation FULL OUTER JOIN非常interable interface,但是而不是每条记录每行的结果展平,它会为您提供occurencesDictionary ,以下操作由您方便

祝你好运!

Spark文档是:http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.rdd.PairRDDFunctions