【问题标题】:Vertex Property Inheritance - Graphx Scala Spark顶点属性继承 - Graphx Scala Spark
【发布时间】:2015-04-20 07:08:02
【问题描述】:

--- 编辑---

我的主要问题是我不理解 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] 类型的图。 我正在寻找一个例子。


【问题讨论】:

    标签: scala apache-spark spark-graphx


    【解决方案1】:

    这将帮助您创建一个二分图,其中顶点属性将帮助您了解不同的类类别。

    // 高级接口 OR 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) 
    

    【讨论】:

      【解决方案2】:

      您可以使用可以合并的消息,例如 Iterable[YourClass]。但是,您必须考虑到此类合并的大小可能会变得非常大。

      【讨论】:

        【解决方案3】:

        这是一个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))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-01-30
          • 2016-01-25
          • 1970-01-01
          • 2020-05-01
          • 2023-03-18
          • 1970-01-01
          • 2014-02-03
          相关资源
          最近更新 更多