【问题标题】:Storing Multiple Columns data in Edge and Vertices in Spark在 Spark 的 Edge 和 Vertices 中存储多列数据
【发布时间】:2020-09-04 09:07:17
【问题描述】:

我是 Spark Graphx 的新手,边的数据框为:

Dataframe : edges_main
+------------------+------------------+------------+--------+-----------+
|               src|               dst|relationship|category|subcategory|
+------------------+------------------+------------+--------+-----------+
|294201130817328347|294201131015844283|   friend   |  school|      class|
|294201131015844283|294201131007361339|  brother   |   home |     cousin|
|294201131015844283|294201131014451003|  son       |   home |   relative|
-------------------------------------------------------------------------

和顶点为:

Dataframe : vertices_main
+------------------+----------+
|               id |value|name|
+------------------+----------+
|294201130817328347|Mary |a   |
|294201131015844283|Hola |b   |
|294201131015844283|Rama |c   |
-------------------------------

我想在 Graphx 中保留其他属性,以便我可以使用 map 访问它们。我的代码:

case class MyEdges(src: String, dst: String, attributes: MyEdgesLabel)
case class MyEdgesLabel(relationship:String,category: String ,subcategory:String)

val edges = edges_main.as[MyEdges].rdd.map { edge =>
      Edge(
        edge.src.toLong,
        edge.dst.toLong,
        //**what to mention here(MyEdgesLabel)**//
      )}

case class MyVerticesLabel(name:String)

val vertices: RDD[(VertexId, Any)] = vertices_data.rdd.map(verticesRow => (
      verticesRow.getLong(0),
      verticesRow.getString(1))
//**what to mention here(MyVerticesLabel)**//
    )

上述要求的原因是创建图表后,我可以通过以下方式直接访问附加属性:

val g = Graph(vertices, edges)
g.vertices.map(v => v._1 + v._2 + /*addidtional attributes which is in case class MyEdgesLabel*/).collect.mkString 
g.edges.map(e =>  e.srcId + e.dstId + e.attr(/*addidtional attributes which is in case class 
 MyVerticesLabel*/))).collect.mkString

我从下面的 url 中得到了一些线索,但我仍然对同时满足顶点和边的多个属性感到困惑: http://www.sunlab.org/teaching/cse6250/fall2019/spark/spark-graphx.html#graph-construction.

请提供相同的帮助。

【问题讨论】:

    标签: scala apache-spark graph spark-graphx


    【解决方案1】:

    您可以使用一个案例类作为边缘属性,另一个作为顶点属性。 MyEdgesLabel 的边缘已经可以了,要创建边缘RDD,只需这样做:

    val edges = edges_main.as[MyEdges].rdd.map { edge =>
          Edge(
            edge.src.toLong,
            edge.dst.toLong,
            MyEdgesLabel(edge.relationship, edge.category, edge.subcategory)
          )}
    

    对于顶点,您需要在案例类中同时包含valuename

    case class MyVerticesLabel(value: String, name: String)
    

    然后用它来创建顶点RDD

    val vertices: RDD[(VertexId, MyVerticesLabel)] = vertices_data.rdd.map{verticesRow => 
        (verticesRow.getAs[Long]("id"),
        MyVerticesLabel(verticesRow.getAs[String]("value"), verticesRow.getAs[String]("name")))
    }
    

    现在,可以轻松访问这些值,例如:

    g.edges.map(e =>  e.srcId + e.dstId + e.attr.relationship).collect.mkString
    

    【讨论】:

    • 除了最后一个g.edges.map 部分外,它工作正常,它没有建立关系。当打印在文本文件中时,它会以MyVerticesLabel(friend,school,class) 的形式出现在e.attr 中。
    • @UtkarshSaraf:与问题中的代码相比,您似乎在做一些不同的事情(因为没有提到油炸、学校和课堂)。如果您使用g.edges.map(_.attr),那么您将获得边的属性,上面是MyEdgesLabel,因为这是我们在创建Edge 时使用的值。也许你在创建边时错误地使用了MyVerticesLabel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多