Scala 火花图形X多重边缘类型
我最近开始使用spark。目前我正在测试一个具有不同顶点和边类型的二部图 根据我在graphx中所做的研究,要有不同的边和一些属性,我需要对边进行子类化 以下是代码片段:Scala 火花图形X多重边缘类型,scala,apache-spark,spark-graphx,Scala,Apache Spark,Spark Graphx,我最近开始使用spark。目前我正在测试一个具有不同顶点和边类型的二部图 根据我在graphx中所做的研究,要有不同的边和一些属性,我需要对边进行子类化 以下是代码片段: scala> trait VertexProperty defined trait VertexProperty scala> case class paperProperty(val paperid: Long, val papername: String, val doi: String, val keywo
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>
任何帮助都将不胜感激您的问题有点徒劳无益,因为GraphX不支持
数据集
,边和顶点都应作为RDD传递,但为了便于讨论:
- 第一个异常是因为Spark中的分布式数据结构是不变的。不要将
用作安装
。只需使用类型注释就可以了
- 出现第二个异常是因为
数据集
进一步受到编码器
使用的限制。数据集中的所有对象
必须使用相同的编码器
在这种情况下,只能使用二进制编码器,而二进制编码器对于用户定义的类不会隐式访问
将这两部分结合起来:
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
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)
edges.rdd