加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 无法声明字符串类型累加器

发布时间:2020-12-16 09:25:04 所属栏目:安全 来源:网络整理
导读:我试图在Scala shell(驱动程序)中定义String类型的累加器变量,但我不断收到以下错误: – scala val myacc = sc.accumulator("Test")console:21: error: could not find implicit value for parameter param: org.apache.spark.AccumulatorParam[String] val
我试图在Scala shell(驱动程序)中定义String类型的累加器变量,但我不断收到以下错误: –

scala> val myacc = sc.accumulator("Test")
<console>:21: error: could not find implicit value for parameter param: org.apache.spark.AccumulatorParam[String]
       val myacc = sc.accumulator("Test")
                                 ^

对于Int或Double类型的累加器来说,这似乎没有问题.

谢谢

解决方法

这是因为Spark默认只提供Long,Double和Float类型的累加器.如果你需要别的东西,你必须扩展AccumulatorParam.

import org.apache.spark.AccumulatorParam

object StringAccumulatorParam extends AccumulatorParam[String] {

    def zero(initialValue: String): String = {
        ""
    }

    def addInPlace(s1: String,s2: String): String = {
        s"$s1 $s2"
    }
}

val stringAccum = sc.accumulator("")(StringAccumulatorParam)

val rdd = sc.parallelize("foo" :: "bar" :: Nil,2)
rdd.foreach(s => stringAccum += s)
stringAccum.value

注意:

通常,您应该避免将累加器用于数据可能随时间显着增长的任务.它的行为类似于分组收集,在最坏的情况下,由于缺乏资源,情况可能会失败.累加器主要用于简单的诊断任务,如跟踪基本统计数据.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读