从另一个 RDD 中减去一个 RDD 不能正常工作

Subtract an RDD from another RDD doesn't work correctly

我想从另一个 RDD 中减去一个 RDD。我查看了文档,发现 subtract 可以做到这一点。实际上,当我测试subtract时,最终的RDD保持不变,并没有删除值!

还有其他功能吗?还是我使用 subtract 不正确?

这是我使用的代码:

 val vertexRDD: org.apache.spark.rdd.RDD[(VertexId, Array[Int])]
 val clusters  = vertexRDD.takeSample(false, 3)
 val clustersRDD: RDD[(VertexId, Array[Int])] = sc.parallelize(clusters)
 val final = vertexRDD.subtract(clustersRDD)
 final.collect().foreach(println(_))

通常不支持或至少不推荐使用可变类型(本例中为数组)执行减法等集合操作。

尝试使用不可变类型。

我相信 WrappedArray 是存储数组的相关容器,但我不确定。

如果您的 rdd 由可变对象组成,它将无法工作...问题是它也不会显示错误,因此这类问题很难识别,我昨天遇到了一个类似的问题,我使用了一种解决方法。

rdd.keyBy( someImmutableValue ) -> do this using the same key value to
 both your rdds

val resultRDD = rdd.subtractByKey(otherRDD).values

Recently I tried the subtract operation of 2 RDDs (of array List) and it is working. The important note is - the RDD val after .subtract method should be the list from where you're subtracting, not the other way around.

正确:val result = theElementYouWantToSubtract.subtract(fromList)

不正确:val reuslt = fromList.subtract(theElementYouWantToSubtract)(不会给出任何 compile/runtime 错误信息)