2015-11-13 85 views
0

我在处理python spark rdd时遇到了一些小问题。我RDD看起来像在Python中组合两条不同的线Spark Spark RDD

old_rdd = [(A1, Vector(V1)), (A2, Vector(V2)), (A3, Vector(V3)), ....]. 

我想用flatMap,从而获得新的RDD,如:

new_rdd = [((A1, A2), (V1, V2)), ((A1, A3), (V1, V3))] and so on. 

问题是flatMap去除元组像[(A1, V1, A2, V2)...].你有带或不带flatMap任何其他建议()。先谢谢你。

+0

什么是模式?,所有组合?排列?或只有一些对? –

+0

这是组合 – Aarav

回答

1

它与Explicit sort in Cartesian transformation in Scala Spark有关。不过,我会假设你已经清理RDD的重复,我将认为ids有一些简单的图案来分析,然后确定,为简单起见,我会想起Lists而不是Vectors

old_rdd = sc.parallelize([(1, [1, -2]), (2, [5, 7]), (3, [8, 23]), (4, [-1, 90])]) 

# It will provide all the permutations, but combinations are a subset of the permutations, so we need to filter. 
combined_rdd = old_rdd.cartesian(old_ 
combinations = combined_rdd.filter(lambda (s1, s2): s1[0] < s2[0]) 

combinations.collect() 

# The output will be... 
# ----------------------------- 
# [((1, [1, -2]), (2, [5, 7])), 
# ((1, [1, -2]), (3, [8, 23])), 
# ((1, [1, -2]), (4, [-1, 90])), 
# ((2, [5, 7]), (3, [8, 23])), 
# ((2, [5, 7]), (4, [-1, 90])), 
# ((3, [8, 23]), (4, [-1, 90]))] 

# Now we need to set the tuple as you want 
combinations = combinations.map(lambda (s1, s1): ((s1[0], s2[0]), (s1[1], s2[1]))).collect() 

# The output will be... 
# ---------------------- 
# [((1, 2), ([1, -2], [5, 7])), 
# ((1, 3), ([1, -2], [8, 23])), 
# ((1, 4), ([1, -2], [-1, 90])), 
# ((2, 3), ([5, 7], [8, 23])), 
# ((2, 4), ([5, 7], [-1, 90])), 
# ((3, 4), ([8, 23], [-1, 90]))] 
+0

阿尔贝托,非常感谢你,你做了我的一天。 – Aarav