【问题标题】:spark graphx multiple edge typesspark graphx 多种边缘类型
【发布时间】:2017-01-16 19:03:37
【问题描述】:

我最近开始使用 spark。目前我正在测试具有不同顶点和边类型的二分图。

根据我在 graphx 中所做的研究,要拥有不同的边和一些属性,我需要对边进行子类化。

这是一个sn-p的代码:

scala> trait VertexProperty
defined trait VertexProperty

scala> case class paperProperty(val paperid: Long, val papername: String, val doi: String, val keywords: String) extends VertexProperty
defined class paperProperty

scala> case class authorProperty(val authorid: Long, val authorname: String) extends VertexProperty
defined class authorProperty

scala> val docsVertces: RDD[(VertexId, VertexProperty)] = docs.rdd.map(x => (x(0).asInstanceOf[VertexId],paperProperty(x(0).asInstanceOf[VertexId],x(1).asInstanceOf[String],x(2).asInstanceOf[String],x(3).asInstanceOf[String])))
docsVertces: org.apache.spark.rdd.RDD[(org.apache.spark.graphx.VertexId, VertexProperty)] = MapPartitionsRDD[23] at map at <console>:47

scala> val authorVertces: RDD[(VertexId, VertexProperty)] = authors.rdd.map(x => (x(0).asInstanceOf[VertexId],authorProperty(x(0).asInstanceOf[Long],x(1).asInstanceOf[String])))
authorVertces: org.apache.spark.rdd.RDD[(org.apache.spark.graphx.VertexId, VertexProperty)] = MapPartitionsRDD[24] at map at <console>:41

scala> val vertices = VertexRDD(docsVertces ++ authorVertces)
vertices: org.apache.spark.graphx.VertexRDD[VertexProperty] = VertexRDDImpl[28] at RDD at VertexRDD.scala:57

scala>

但是我在边缘上失败了。

scala> class EdgeProperty()
defined class EdgeProperty

scala> case class authorEdgeProperty( val doccount: Long) extends  EdgeProperty()
defined class authorEdgeProperty

scala> case class citeEdgeProperty() extends  EdgeProperty()
defined class citeEdgeProperty

scala> // edge using subclass will not work we need to have one consistent superclass

scala> val docauthoredges = docauthor.map(x => Edge(x(0).asInstanceOf[VertexId],x(1).asInstanceOf[VertexId],     authorEdgeProperty(x(1).asInstanceOf[Long])))
docauthoredges: org.apache.spark.sql.Dataset[org.apache.spark.graphx.Edge[authorEdgeProperty]] = [srcId: bigint, dstId: bigint ... 1 more field]

scala> val docciteedges = doccites.map(x => Edge(x(0).asInstanceOf[VertexId],x(1).asInstanceOf[VertexId], citeEdgeProperty()))
docciteedges: org.apache.spark.sql.Dataset[org.apache.spark.graphx.Edge[citeEdgeProperty]] = [srcId: bigint, dstId: bigint ... 1 more field]

scala> docauthoredges.unionAll(docciteedges)
<console>:52: error: type mismatch;
 found   :    org.apache.spark.sql.Dataset[org.apache.spark.graphx.Edge[citeEdgeProperty]]
 required: org.apache.spark.sql.Dataset[org.apache.spark.graphx.Edge[authorEdgeProperty]]
       docauthoredges.unionAll(docciteedges)
                               ^

scala>

我尝试将优势投射到他们的超类并收到以下消息:

scala> val docauthoredges = docauthor.map(x => Edge(x(0).asInstanceOf[VertexId],x(1).asInstanceOf[VertexId],         authorEdgeProperty(x(1).asInstanceOf[Long]).asInstanceOf[EdgeProperty]))
java.lang.UnsupportedOperationException: No Encoder found for EdgeProperty
- field (class: "EdgeProperty", name: "attr")
- root class: "org.apache.spark.graphx.Edge"
  at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor(ScalaReflection.scala:598)
...

任何帮助将不胜感激

【问题讨论】:

    标签: scala apache-spark spark-graphx


    【解决方案1】:

    你的问题有点徒劳,因为 GraphX 不支持Datasets,边和顶点都应该作为RDDs 传递,但是为了论证:

    • 您会遇到第一个异常,因为 Spark 中的分布式数据结构是不变的。不要使用asInstanceOf。只需明确使用类型注释即可。
    • 您会遇到第二个异常,因为Datasets 的使用进一步受到Encoders 的限制。 Dataset 中的所有对象都必须使用相同的 Encoder,在这种情况下,只有使用二进制编码器才能实现,而用户定义的类不能隐式访问。

    将这两部分结合起来:

    import org.apache.spark.sql.{Dataset, Encoders}
    
    sealed trait EdgeProperty
    
    case class AuthorEdgeProperty(val doccount: Long) extends  EdgeProperty
    case class CiteEdgeProperty() extends EdgeProperty
    
    val docauthoredges: Dataset[EdgeProperty] = spark.range(10)
      .map(AuthorEdgeProperty(_): EdgeProperty)(Encoders.kryo[EdgeProperty])
    
    val docciteedges: Dataset[EdgeProperty] = spark.range(5)
      .map(_ => CiteEdgeProperty(): EdgeProperty)(Encoders.kryo[EdgeProperty])
    
    val edges: Dataset[EdgeProperty] = docauthoredges.union(docciteedges)
    

    转换为 RDD 以使其在 GraphX 中可用:

    edges.rdd
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多