[英]Type mismatch in Scala Map from getOrElse returning Equals
I have this Scala snippet from a custom mapper (for use in a Spark mapPartitions
) I'm writing to compute histograms of multiple Int
fields simultaneously. 我正在编写一个自定义映射器(用于Spark
mapPartitions
)中的Scala代码片段,以同时计算多个Int
字段的直方图。
def multiFeatureHistogramFunc(iter: Iterator[Row]) : Iterator[(Int, (Int, Long))] = {
var featureHistMap:Map[Int, (Int, Long)] = Map()
while (iter.hasNext)
{
val cur = iter.next;
indices.foreach( { index:Int =>
val v:Int = if ( cur.isNullAt(index) ) -100 else cur.getInt(index)
var featureHist:Map[Int, Long] = featureHistMap.getOrElse(index, Map())
val newCount = featureHist.getOrElse(v,0L) + 1L
featureHist += (v -> newCount)
featureHistMap += (index -> featureHist)
})
}
featureHistMap.iterator
}
But the error I'm getting is this: 但是我得到的错误是这样的:
<console>:49: error: type mismatch;
found : Equals
required: Map[Int,Long]
var featureHist:Map[Int, Long] =
featureHistMap.getOrElse(index, Map())
^
I couldn't find the answer to this specific issue. 我找不到这个特定问题的答案。 It looks to me like the default parameter in
featureHistMap.getOrElse
is a different type than the value field of the featureHistMap
itself and the common parent type is Equals
so this causes a type mismatch. 在我看来,
featureHistMap.getOrElse
的默认参数与featureHistMap
本身的值字段不同,并且其公共父类型为Equals
因此会导致类型不匹配。 I tried a number of different things like changing the default parameter to be a more specific type, but this just caused a different error. 我尝试了许多不同的操作,例如将默认参数更改为更特定的类型,但这只是导致了另一个错误。
Can someone explain what's going on here and how to fix it? 有人可以解释这里发生了什么以及如何解决吗?
The problem is that you declared your featureHistMap
as Map[Int, (Int, Long)]
- note that you are mapping an Int
to a pair (Int, Long)
. 问题是您将
featureHistMap
声明为Map[Int, (Int, Long)]
-请注意,您正在将一个Int
映射到一个对(Int, Long)
。 Later, you try to retrieve a value from it as a Map[Int, Long]
, instead of a pair (Int, Long)
. 稍后,您尝试从其中检索一个值作为
Map[Int, Long]
,而不是一对(Int, Long)
。
You either need to redeclare the type of featureHistMap
to Map[Int, Map[Int, Long]]
, or the type of featureHist
to (Int, Long)
. 您需要将
featureHistMap
的类型重新声明为Map[Int, Map[Int, Long]]
,或者将featureHist
的类型featureHist
为(Int, Long)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.