2017-03-05 53 views
1

//我用星火2.01 //如何在火花执行这种类型的乘积的2.01

我的数据看起来,

(K1,Array(V1,V2,V3.....V30)) 
(K2,Array(V1,V2,V3.....V30)) 
(K3,Array(V1,V2,V3.....V30)) 
... 
(K3704, Array(V1,V2,V3.....V30)) 

,我想创建一个每个键值的笛卡尔产品列表值。

(K1, (V1,V2),(V1,V3),(V1,V4) ... 
(K2, (V2,V3),(V2,V4),(V2,V5) ... 
... 
//PS. there are no duplicate elements like (V1,V2) == (V2,V1) 

我想会有一个30!每个键的操作,但如果可以优化它会更好。

回答

1

在Python,我们可以使用combinations()功能从包itertools内部mapValues()

from itertools import combinations 
rdd.mapValues(lambda x: list(combinations(x, 2))) 

在Scala中,我们可以以类似的方式使用combinations()方法。但由于它只是摄取和输出对象类型Seq,我们必须一起链一对夫妇更多的方法来为您预期的格式到达:

rdd.mapValues(_.toSeq.combinations(2).toArray.map{case Seq(x,y) => (x,y)}) 
+0

谢谢!它帮助了很多!^- ^ –