2016-05-27 21 views
4

我正在使用spark-shell来运行我的代码。在我的代码中,我定义了一个函数,并使用它的参数调用该函数。Spark中的“错误:类型不匹配”,找到和需要的数据类型相同

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

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)) 

该代码无错地运行,直到最后一行,它得到上述错误。

回答

4

这里是诀窍。让打开REPL和定义一个类:

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

并且这个类操作的简单函数:

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

重新定义类:

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

和创建一个实例:

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

全部wh ats左是致电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状态,您应该跟踪您创建的实体,并且如果类型定义更改,请在继续之前确保没有歧义的定义持续存在(如果需要,请覆盖事项)。

+0

从这个答案中不清楚原作者应该如何解决他的问题。这似乎只显示如何使用不同的代码创建类似的错误消息。 – GeorgeLewis

+0

@GeorgeLewis感谢您的反馈。这是一种预期的行为。只是错误信息不是很清楚。除了保持REPL的状态清理和删除/覆盖模糊的定义之外,这里没有任何事情可做。 – zero323