如何更换RDD的元素 [英] How to replace the elements of an RDD
问题描述
我有一个RDD像这样:
I have an RDD like so:
JavaPairRDD< SubspaceFlag,可迭代<点和GT;> flagPointPairs
示范值:
(01),[(5,5),(6,1),(7,2),(9,4)]
点
是一个(X,Y)点在二维空间
Point
is an (x, y) point in a two-dimensional space
我需要删除从可迭代&LT的一些元素;点>
的 Tuple2的一部分
。例如,我只需要保留的点独领风骚的其他(X1< = X2和Y1< Y2或Y1< = Y2和X1< X2)
。
I need to remove some elements from the Iterable<Point>
part of the Tuple2
. For example I need to keep only the points that dominate the others (x1 <= x2 AND y1 < y2 OR y1 <= y2 AND x1 < x2)
.
在结束我留下的临时列表 [(5,5),(6,1)]
我想转换回 JavaPairRDD&LT; SubspaceFlag,可迭代&LT;点&GT;&GT;
- > (01),[(5,5),(6,1)]
。
In the end I am left with a temporary list [(5, 5), (6, 1)]
which I want to convert back to JavaPairRDD<SubspaceFlag, Iterable<Point>>
-> (01), [(5, 5), (6, 1)]
.
我知道如何访问可迭代&LT;点&GT;
,我知道如何让我感兴趣从可迭代&LT较小的名单;点&GT;
,但我不知道如何使该列表回到一个RDD与它的伴随 SubspaceFlag
。
I know how to access the Iterable<Point>
, I know how to make the smaller list that interests me from the Iterable<Point>
, but I don't know how to make that list back to an RDD with it's accompanying SubspaceFlag
.
推荐答案
RDDS是不可改变的结构,因此你不要在RDD替换元素,你转换一个RDD到另一个。
在这种特定的情况下, rdd.map
穿越 - 可以让你的函数适用于RDD中的每个元素,产生了新的RDD。
RDDs are immutable structures, hence you don't replace elements in an RDD, you transform an RDD into another.
In this specific case, the rdd.map
tranformation will allow you to apply a function to each element of the RDD, resulting in a new RDD.
由于
val flagPointPairsRdd:RDD[SubspaceFlag, Iterable[Point]] = ???
和一个功能:
def dominants(points: Iterable[Point]):Iterable[Point] = ??? //Your impl here
然后就可以通过获得期望的结果:
Then you can obtain the desired outcome by:
val dominatingPairsRdd = flagPointPairs.map{case (flag, points) => (flag, dominants(points))}
相应的Java code是相当类似(只是更详细),并作为练习留给读者。
The equivalent Java code is fairly similar (only more verbose) and left as an exercise to the reader.
这篇关于如何更换RDD的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!