scala - Spark 中的 "error: type mismatch"具有相同的找到和所需的数据类型

标签 scala apache-spark spark-graphx

我正在使用 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 并定义一个类:

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)

剩下的就是调用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 声明您应该跟踪您创建的实体,如果类型定义发生更改,请确保在继续之前没有不明确的定义持续存在(如果需要,覆盖事物)。

关于scala - Spark 中的 "error: type mismatch"具有相同的找到和所需的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37476790/

相关文章:

list - 在 scala 列表中查找元素并知道满足哪个谓词

hbase - GraphX - 存储和计算超过 30 亿个顶点的最佳方式

scala - 非持久化 RDD 如何导致 RPC 超时?

scala - reduceByKey 处理每个 flatMap 输出而不聚合 GraphX 中键的值

scala - 如何使用 Long 数据类型在 Apache Spark GraphX 中创建 VertexId?

scala - 倾斜的窗口函数和 Hive 源分区?

使用 Cassandra 进行 Java Spark 流处理

scala - Scala 与 Akka 中的相互身份验证

scala - 如何使用apache spark在mysql数据库中创建表

apache-spark - Spark如何处理HADOOP_CONF_DIR?