声明一个 Edge Graphx 类型的空数组

Declaring an empty array of type Edge Graphx

我正在从文件中读取数据以创建图形的边。我已经声明了一个数组并在其中一个接一个地添加边。此代码运行良好:

class AIRecipes() 
case class edgeProperty(val relation: String, val usedIn: String) extends AIRecipes

var edgeArray = Array(Edge(0L, 0L, edgeProperty("", "")))
edgeArray = edgeArray ++ Array(Edge(VertexId, VertexId, edgeProperty("", "")) )

但在第一行,我不想声明一个带有虚拟值的额外边,而是想声明一个空数组:

var edgeArray = Array.empty[Edge[(Long, Long, Object)]]
edgeArray = edgeArray ++ Array(Edge(VertexId, VertexId, edgeProperty("", "")) )

但它给了我以下关于“++”的编译错误:

type mismatch; found : Array[org.apache.spark.graphx.Edge[_ >: (Long, Long, Object) with net.sansa_stack.template.spark.rdf.TripleReader.edgeProperty <: Product with Serializable]] required: Array[org.apache.spark.graphx.Edge[(Long, Long, Object)]] Note: org.apache.spark.graphx.Edge[_ >: (Long, Long, Object) with net.sansa_stack.template.spark.rdf.TripleReader.edgeProperty <: Product with Serializable] >: org.apache.spark.graphx.Edge[(Long, Long, Object)], but class Array is invariant in type T. You may wish to investigate a wildcard type such as _ >: org.apache.spark.graphx.Edge[(Long, Long, Object)].

我也试过这个:

edgeArray :+ Array(Edge(VertexId, VertexId, edgeProperty("", "")) )

它没有给我编译错误,但数组中没有添加任何内容。

第一个数组的类型不正确。请注意 Edge 仅由其 属性 参数化,因此您尝试合并的表达式类型为 Array[Edge[edgeProperty]]:

scala> :t Array(Edge(0L, 0L, edgeProperty("", "")))
Array[org.apache.spark.graphx.Edge[edgeProperty]]

而您将变量定义为 Array.empty[Edge[(Long, Long, Object)]]

Object部分是第二个问题。正如您在异常消息中看到的那样,Array(与任何其他可变容器一样)是不变的。因此,如果您真的想使用 Object,则必须:

scala> var edgeArray = Array.empty[Edge[Object]]
edgeArray: Array[org.apache.spark.graphx.Edge[Object]] = Array()

scala> edgeArray = edgeArray ++ (Array(Edge(1L, 2L, edgeProperty("", "")) ): Array[Edge[Object]])
edgeArray: Array[org.apache.spark.graphx.Edge[Object]] = [Lorg.apache.spark.graphx.Edge;@338

但我还是推荐

scala> var edgeArray = Array.empty[Edge[edgeProperty]]
edgeArray: Array[org.apache.spark.graphx.Edge[edgeProperty]] = Array()

scala> edgeArray = edgeArray ++ Array(Edge(1L, 2L, edgeProperty("", "")) )
edgeArray: Array[org.apache.spark.graphx.Edge[edgeProperty]] = [Lorg.apache.spark.graphx.Edge;@7d59e8d4