顶点 属性 继承 - Graphx Scala Spark
Vertex Property Inheritance - Graphx Scala Spark
--- 编辑 ---
我的主要问题是我不理解 Graphx 文档中给出的这一段:
在某些情况下,可能希望在同一个图中具有不同 属性 类型的顶点。这可以通过继承来实现。例如,将用户和产品建模为二分图,我们可以执行以下操作:
class VertexProperty()
case class UserProperty(val name: String) extends VertexProperty
case class ProductProperty(val name: String, val price: Double) extends VertexProperty
// The graph might then have the type:
var graph: Graph[VertexProperty, String] = null
在上面的例子中,给出了每个 UserProperty 和 ProductProperty 的 RDD 以及一个 EdgeProperty 的 RDD,如何创建一个 Graph[VertexProperty, String] 类型的图。
我正在寻找一个例子。
您可以使用可以合并的消息,例如 Iterable[YourClass]。但是,您必须考虑到此类合并的规模可能会变得非常大。
这是一个scala问题,只需使用asInstanceOf将扩展类型转换为抽象类型即可,例如:
val variable1: RDD[UserProperty] = {..your code..}
val variable2: RDD[ProductProperty] = {..your code..}
val result: RDD[VertexProperty] = SparkContext.union(
variable1.asInstanceOf[VertexProperty],
variable2.asInstanceOf[VertexProperty])
边属性同理,使用
val edge: EdgeProperty = Edge(srcID, dstID, variable.asInstanceOf(EdgeProperty))
这将帮助您创建二分图,其中顶点 属性 将帮助您理解不同的 class 类别。
// 高级接口或 VertexProperty
trait Node { def getVertexID : Long }
class UserNode(sID: String, sname : String, sAge) extends Node with Serializable { }
class ProductNode(sID: String, sNO : String, sdoe : String) extends Node with Serializable{ }
// 数据加载
val users: RDD[Node] = sc.textFile("users.txt")
.map { row => val cols = row.split(",")
( new UserNode(cols(0), cols(1), cols(2))
}
val products: RDD[Node] = sc.textFile("products.txt")
.map { row => val cols = row.split(",")
( new ProductNode(cols(0), cols(1), cols(3)))
}
// 加入两个RDD
val nodes : RDD[Node] = users.++(products)
--- 编辑 ---
我的主要问题是我不理解 Graphx 文档中给出的这一段:
在某些情况下,可能希望在同一个图中具有不同 属性 类型的顶点。这可以通过继承来实现。例如,将用户和产品建模为二分图,我们可以执行以下操作:
class VertexProperty()
case class UserProperty(val name: String) extends VertexProperty
case class ProductProperty(val name: String, val price: Double) extends VertexProperty
// The graph might then have the type:
var graph: Graph[VertexProperty, String] = null
在上面的例子中,给出了每个 UserProperty 和 ProductProperty 的 RDD 以及一个 EdgeProperty 的 RDD,如何创建一个 Graph[VertexProperty, String] 类型的图。 我正在寻找一个例子。
您可以使用可以合并的消息,例如 Iterable[YourClass]。但是,您必须考虑到此类合并的规模可能会变得非常大。
这是一个scala问题,只需使用asInstanceOf将扩展类型转换为抽象类型即可,例如:
val variable1: RDD[UserProperty] = {..your code..}
val variable2: RDD[ProductProperty] = {..your code..}
val result: RDD[VertexProperty] = SparkContext.union(
variable1.asInstanceOf[VertexProperty],
variable2.asInstanceOf[VertexProperty])
边属性同理,使用
val edge: EdgeProperty = Edge(srcID, dstID, variable.asInstanceOf(EdgeProperty))
这将帮助您创建二分图,其中顶点 属性 将帮助您理解不同的 class 类别。
// 高级接口或 VertexProperty
trait Node { def getVertexID : Long }
class UserNode(sID: String, sname : String, sAge) extends Node with Serializable { }
class ProductNode(sID: String, sNO : String, sdoe : String) extends Node with Serializable{ }
// 数据加载
val users: RDD[Node] = sc.textFile("users.txt")
.map { row => val cols = row.split(",")
( new UserNode(cols(0), cols(1), cols(2))
}
val products: RDD[Node] = sc.textFile("products.txt")
.map { row => val cols = row.split(",")
( new ProductNode(cols(0), cols(1), cols(3)))
}
// 加入两个RDD
val nodes : RDD[Node] = users.++(products)