代码之家  ›  专栏  ›  技术社区  ›  user1342124

在scala中执行map reduce时清空集合

  •  0
  • user1342124  · 技术社区  · 7 年前

    我遇到一个spark作业正在退出,错误消息为空集合。

    java.lang.UnsupportedOperationException: empty collection
    

    我已放大到导致问题的两行。

    sum_attribute1 = inputRDD.map(_.attribute1).reduce(_+_)
    sum_attribute2 = inputRDD.map(_.attribute2).reduce(_+_)`
    

    其他行可以。映射和。不同的计数很好。 我喜欢打印输入。映射(属性1)和输入。map(.attribute2)查看reduce之前的映射。

    我想我可以定义如下

    sum_attribute1 = inputRDD.map(_.attribute1)
    

    但当我试图编译代码时,它显示出错误:

    [error]  found   : org.apache.spark.rdd.RDD[Int]
    [error]  required: Long
    [error] sum_attribute1 = inputRDD.map(_.attribute1)
    [error]                              ^
    

    我的attribute1被定义为Int,但当我试图将其定义为Long时,它给了我另一个错误。

    我走的方向对吗? 如何在映射之后和还原之前打印数据? 空集合可能存在什么问题? “”中的下划线是什么。attribute1和reduce(\+\)平均值?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Frederic A.    7 年前

    我认为你没有朝着正确的方向前进,我将重点关注以下要素:

    我建议您先学习一点scala。对于您的一个具体问题,请阅读 about that usage of _

    关于你的另一个问题, reduce 无法用于空集合,建议使用 fold 相反,因为它支持空集合。