"error: type mismatch" in Spark with same found and required datatypes

"error: type mismatch" in Spark with same found and required datatypes

我正在为 运行 我的代码使用 spark-shell。在我的代码中,我定义了一个函数并使用它的参数调用该函数。

问题是我在调用该函数时出现以下错误。

error: type mismatch;

found   : org.apache.spark.graphx.Graph[VertexProperty(in class $iwC)(in class $iwC)(in class $iwC)(in class $iwC),String]

required: org.apache.spark.graphx.Graph[VertexProperty(in class $iwC)(in class $iwC)(in class $iwC)(in class $iwC),String]

此错误背后的原因是什么?它与 Spark 中的 Graph 数据类型有什么关系吗?

代码:这是我的代码的一部分,涉及函数的定义和调用"countpermissions"。

class VertexProperty(val id:Long) extends Serializable
case class User(val userId:Long, val userCode:String, val Name:String, val Surname:String) extends VertexProperty(userId)
case class Entitlement(val entitlementId:Long, val name:String) extends VertexProperty(entitlementId)

def countpermissions(es:String, sg:Graph[VertexProperty,String]):Long = {
    return 0
}

val triplets = graph.triplets
val temp = triplets.map(t => t.attr)
val distinct_edge_string = temp.distinct    
var bcast_graph = sc.broadcast(graph)        
val edge_string_subgraph = distinct_edge_string.map(es => es -> bcast_graph.value.subgraph(epred = t => t.attr == es))
val temp1 = edge_string_subgraph.map(t => t._1 -> countpermissions(t._1, t._2))

代码运行没有错误,直到最后一行出现上述错误。

这是诀窍。让我们打开 REPL 并定义一个 class:

scala> case class Foo(i: Int)
defined class Foo

和一个在此 class 上运行的简单函数:

scala> def fooToInt(foo: Foo) = foo.i
fooToInt: (foo: Foo)Int

重新定义 class:

scala> case class Foo(i: Int)
defined class Foo

并创建一个实例:

scala> val foo = Foo(1)
foo: Foo = Foo(1)

剩下的就是调用 fooToInt:

scala> fooToInt(foo)
<console>:34: error: type mismatch;
 found   : Foo(in class $iwC)(in class $iwC)(in class $iwC)(in class $iwC)
 required: Foo(in class $iwC)(in class $iwC)(in class $iwC)(in class $iwC)
          fooToInt(foo)

是不是很眼熟?另一个更好地了解正在发生的事情的技巧:

scala> case class Foo(i: Int)
defined class Foo

scala> val foo = Foo(1)
foo: Foo = Foo(1)

scala> case class Foo(i: Int)
defined class Foo

scala> def fooToInt(foo: Foo) = foo.i
<console>:31: error: reference to Foo is ambiguous;
it is imported twice in the same scope by
import INSTANCE.Foo
and import INSTANCE.Foo
         def fooToInt(foo: Foo) = foo.i

长话短说,这是一种预期的行为,虽然有点令人困惑,但它是由同一范围内存在的模棱两可的定义引起的。

除非你想定期 :reset REPL 声明你应该跟踪你创建的实体,如果类型定义发生变化,请确保在你继续之前没有歧义定义(如果需要覆盖)。