【问题标题】:unable to write data to RDD无法将数据写入 RDD
【发布时间】:2019-04-30 14:18:40
【问题描述】:

我在 windows 上运行 pyspark 并实现了以前在 python 中实现的算法

我已将列表转换为 RDD,但出现错误

voxel =[[ ['0' for col in range(100)] for col in range(100)] for row in range(6)] 
delay = sc.parallelize(voxel,4)
locationforant=[[75,15],[100,50],[75,80],[25,80],[0,50],[25,15]]
for a in range(6):
    for x in range(100):
        for y in range(100):
            distance=((math.sqrt((x-locationforant[a][0])**2+(y-locationforant[a][1])**2))/100)*200
            delay[a][x][y] = round(distance)

TypeError                                 Traceback (most recent call last)
<ipython-input-9-b81a67e36764> in <module>()
     21             distance=((math.sqrt((x-locationforant[a][0])**2+(y-locationforant[a][1])**2))/100)*200
     22 #             breakpoint()
---> 23             delay[a][x][y] = round(distance)

TypeError: 'RDD' object does not support indexing

【问题讨论】:

  • Spark 是分布式的,因此即使是三重嵌套的 for 循环也不适用于 Spark,因为它将在运行 Spark 执行器的每台机器上执行。此外,RDD 根本无法像 Python 列表那样访问。首先将您的代码转换为 Pandas 数据帧,这将更接近于获得类似 Spark 的算法
  • 谢谢,我使用了 map 函数,它以某种方式解决了问题,但是 pyspark 中嵌套循环的替代解决方案是什么?抱歉,我是 spark 新手。

标签: python apache-spark pyspark rdd


【解决方案1】:

如错误所示,您不能在 RDD 上使用索引运算符 ([])。您需要使用 map 之类的东西将函数应用于每一行。

【讨论】:

  • 我很高兴它有帮助。
猜你喜欢
  • 2018-04-15
  • 1970-01-01
  • 2017-12-28
  • 2015-04-07
  • 2017-06-05
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 2018-05-08
相关资源
最近更新 更多