如何在PySpark中压缩两个RDD?

时间:2017-01-24 00:38:30

标签: scala hadoop apache-spark pyspark rdd

我一直在尝试将两个Rdds合并到averagePoints1和kpoints2之下。它一直在抛出这个错误

ValueError: Can not deserialize RDD with different number of items in pair: (2, 1)

我试了很多东西,但我不能两个Rdd相同,分区数量相同。我的下一步是在两个列表上应用欧几里德距离函数来衡量差异,所以如果有人知道如何解决这个错误,或者我可以遵循不同的方法,我会非常感激。

提前致谢

 averagePoints1 = averagePoints.map(lambda x: x[1])
 averagePoints1.collect()
 Out[15]:
 [[34.48939954847243, -118.17286894440112],
 [41.028994230117945, -120.46279399895184],
 [37.41157578999635, -121.60431843383599],
 [34.42627845075509, -113.87191272382309],
 [39.00897622397381, -122.63680410846844]] 

  kpoints2 = sc.parallelize(kpoints,4)
  In [17]:

  kpoints2.collect()
  Out[17]:
  [[34.0830381107, -117.960562808],
  [38.8057258629, -120.990763316],
  [38.0822414157, -121.956922473],
  [33.4516748053, -116.592291648],
  [38.1808762414, -122.246825578]]

2 个答案:

答案 0 :(得分:2)

a= [[34.48939954847243, -118.17286894440112],
 [41.028994230117945, -120.46279399895184],
 [37.41157578999635, -121.60431843383599],
 [34.42627845075509, -113.87191272382309],
 [39.00897622397381, -122.63680410846844]] 
b= [[34.0830381107, -117.960562808],
  [38.8057258629, -120.990763316],
  [38.0822414157, -121.956922473],
  [33.4516748053, -116.592291648],
  [38.1808762414, -122.246825578]]

rdda = sc.parallelize(a)
rddb = sc.parallelize(b)
c = rdda.zip(rddb)
print(c.collect())

检查这个答案 Combine two RDDs in pyspark

答案 1 :(得分:0)

newSample=newCenters.collect() #new centers as a list
    samples=zip(newSample,sample) #sample=> old centers
    samples1=sc.parallelize(samples)
    totalDistance=samples1.map(lambda (x,y):distanceSquared(x[1],y))

对于未来的搜索者来说,这是我最后遵循的解决方案