比较两个 RDD [英] Comparing two RDDs
问题描述
我有两个 RDD[Array[String]],我们称它们为 rdd1 和 rdd2.我将创建一个新的 RDD,其中仅包含 rdd2 的条目,而不是 rdd1(基于键).我通过 Intellij 在 Scala 上使用 Spark.
I have two RDD[Array[String]], let's call them rdd1 and rdd2. I would create a new RDD containing just the entries of rdd2 not in rdd1 (based on a key). I use Spark on Scala via Intellij.
我用一个键将rdd1和rdd2分组(我将只比较两个rdds的键):
I grouped rdd1 and rdd2 by a key (I will compare just the keys of the two rdds):
val rdd1Grouped = rdd1.groupBy(line => line(0))
val rdd2Grouped = rdd2.groupBy(line => line(0))
然后,我使用了 leftOuterJoin
:
val output = rdd1Grouped.leftOuterJoin(rdd2Grouped).collect {
case (k, (v, None)) => (k, v)
}
但这似乎没有给出正确的结果.
but this doesn't seem to give the correct result.
它有什么问题?有什么建议吗?
What's wrong with it? Any suggests?
RDDS 示例(每一行都是一个 Array[String],ofc):
rdd1 rdd2 output (in some form)
1,18/6/2016 2,9/6/2016 2,9/6/2016
1,18/6/2016 2,9/6/2016
1,18/6/2016 2,9/6/2016
1,18/6/2016 2,9/6/2016
1,18/6/2016 1,20/6/2016
3,18/6/2016 1,20/6/2016
3,18/6/2016 1,20/6/2016
3,18/6/2016
3,18/6/2016
3,18/6/2016
在这种情况下,我只想添加条目2,9/6/2016",因为键2"不在 rdd1 中.
In this case I wanna add just the entry "2,9/6/2016" because the key "2" is not in rdd1.
推荐答案
新的 RDD 仅包含 rdd2 的条目,不在 rdd1 中
new RDD containing just the entries of rdd2 not in rdd1
left join 将保留 rdd1 中的所有键并附加 RDD2 匹配键值的列.所以显然左连接/外连接不是解决方案.
left join would retain all keys in rdd1 and append columns of RDD2 matching key values. So clearly left join/outer join is not the solution.
rdd1Grouped.subtractByKey(rdd2Grouped)
适合您的情况.
附:: 另请注意,如果 rdd1 较小,则更好地广播它.这样,在减法时只会流式传输第二个 rdd.
P.S. : Also note that if rdd1 is smaller better broadcast it. In that way, only second rdd would be streamed at the time of subtract.
这篇关于比较两个 RDD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!