【问题标题】:PySpark function behaving different between mapValues and filterPySpark 函数在 mapValues 和过滤器之间表现不同
【发布时间】:2015-07-08 15:08:26
【问题描述】:

我正在使用 pyspark 在 spark 中工作。当我使用下面的 lambda 时,我有一个格式为 [(key, (num, (min, max, count))),....] 的 rdd

t = fullBids.filter(lambda (value, stats): (stats[2] > 10 and stats[0] < value and value < stats[1]))

出错
tuple index out of range

但是当我在 mapValues 调用中使用它时,它会成功运行,正确返回 True 或 False。

ti = fullBids.mapValues(lambda (value, stats): (stats[2] > 10 and stats[0] < value and value < stats[1]))

我希望过滤器能够正常工作,但事实并非如此。有人可以解释我在这里缺少什么吗?

【问题讨论】:

    标签: python apache-spark pyspark


    【解决方案1】:

    如果你分解你的 RDD 格式

    (key, (num, (min, max, count)))
    
    key = value
    (num, (min, max, count)) = stats
    num = stats[0]
    (min, max, count) = stats[1]
    min = stats[1][0]
    max = stats[1][1]
    count = stats[1][2]
    

    所以你的stats[2] 超出范围

    【讨论】:

      【解决方案2】:

      当您调用filter 时,value 是键值对 RDD 的键,而 stats 是 RDD 的值 ((num, (min, max, count))),这就是您有 tuple index out of range 的原因。

      当您调用 mapValues 时,valuenumstats(min, max, count)。事实上,mapValues 转换传递了键值对 RDD 中的每个值。

      【讨论】:

      • 谢谢。这很有帮助。对我来说,在 rdd 函数中传递了什么并不明显。是否有很好的信息来源?
      • 我也遇到了同样的问题,建议你看《Learning Spark》这本书,非常清晰,里面有很多python、scala和java的例子。
      猜你喜欢
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多