4

我想从另一个 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(_))
4

3 回答 3

3

通常不支持或至少不推荐使用可变类型(在此示例中为数组)执行诸如减法之类的集合操作。

尝试改用不可变类型。

我相信WrappedArray是用于在集合中存储数组的相关容器,但我不确定。

于 2015-06-14T14:51:50.060 回答
2

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

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

val resultRDD = rdd.subtractByKey(otherRDD).values
于 2016-05-18T09:17:15.360 回答
1

最近我尝试了 2 个 RDD(数组列表)的减法运算,它正在工作。重要的注意事项是 - .subtract 方法之后的 RDD val 应该是您要减去的列表,而不是相反。

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

不正确:(val reuslt = fromList.subtract(theElementYouWantToSubtract)不会给出任何编译/运行时错误消息)

于 2018-01-12T20:23:48.077 回答