gpt4 book ai didi

scala - Apache Spark : Hashmap accumulators give type mismatch error

转载 作者:行者123 更新时间:2023-12-02 15:39:10 27 4
gpt4 key购买 nike

我试图将 HashMap 声明为 Spark (v0.9.1) 累加器。文档指出“Spark 本身支持数值类型的累加器和标准可变集合...”( link )。

但是,当我尝试创建 HashMap[String, Boolean] 时,这似乎对我不起作用:

scala> import collection.mutable.HashMap
import collection.mutable.HashMap

scala> val accum = sc.accumulator("test" -> true)(HashMap)
<console>:13: error: type mismatch;
found : scala.collection.mutable.HashMap.type
required: org.apache.spark.AccumulatorParam[(String, Boolean)]
val accum = sc.accumulator("test" -> true)(HashMap)

最佳答案

首先,您应该传递一个实际的 HashMap[String, String] 而不是 (String, String):

sc.accumulator(HashMap("t"-> true))

并且您可能需要编写自己的累加器,因为我没有找到 HashMap 的开箱即用的隐式:

implicit object iHashMap extends AccumulatorParam[HashMap[String, Boolean]] {
def zero(m: HashMap[String, String]) = HashMap()
def addInPlace(m1: HashMap[String, Boolean], m2: HashMap[String, Boolean]) = m1 ++ m2
}

这种行为可能不是您个人所期望的,但我希望您能捕获要点。

您还可以在这里找到一些非常有用的示例:https://github.com/apache/spark/blob/60abc252545ec7a5d59957a32e764cd18f6c16b4/core/src/test/scala/org/apache/spark/AccumulatorSuite.scala

关于scala - Apache Spark : Hashmap accumulators give type mismatch error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23414238/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com