3

I'd like to know can I model a GraphX graph with different types of vertices? Say I have the following entities: product, buyer, seller. I want to form a graph structure with these entities as vertices. (eg: show graphically a product being sold by a seller and bought by a buyer.) Is it possible with GraphX and if so, how? Thanks!

Mayuri M.
  • 121
  • 1
  • 2

1 Answers1

9

Of course.

A vertex is represented using an id and a set of properties, which can differ between vertices. Like so:

val vertices=Array(
  ( 1L, ( "1 property" ) ),
  ( 2L, ( "2 properties", 2 ) ),
  ( 3L, ( "3 properties", 3, true ) )
)
val vRDD= spark.parallelize( vertices )
val edges = Array(Edge(1L,2L,1800),Edge(2L,3L,800),Edge(3L,1L,1400))
val eRDD= spark.parallelize( edges )

val graph = Graph( vRDD, eRDD )

graph.vertices.collect.foreach( println )

You could use the first property of each vertex to denote what type of vertex it is.

Or you can use a more formal approach approach:

class VertexProperty()
case class DocumentProperty( val url:   String ) extends VertexProperty
case class UserProperty(     val name:  String ) extends VertexProperty
case class SentenceProperty( val index: Int    ) extends VertexProperty

val vertices = Array[ ( VertexId, VertexProperty) ] (
  ( 1L, DocumentProperty("www.bbc.co.uk") ),
  ( 2L, UserProperty("Sam") ),
  ( 3L, SentenceProperty( 1 ) )
)
val vRDD= spark.parallelize(vertices)

val edges = Array( Edge( 1L, 2L, 1800 ), Edge( 2L, 3L, 800 ), Edge( 3L, 1L, 1400 ) )
val eRDD= spark.parallelize(edges)

var graph: Graph[ VertexProperty, Int ] = Graph( vRDD, eRDD )

graph.vertices.collect.foreach {
  case ( id, DocumentProperty( url ) ) => println( s"$url" )
  case _ =>
}
Izhaki
  • 23,372
  • 9
  • 69
  • 107
  • Great answer, btw, what if properties are not needed at all and IDs are more than enough? Could you extend your answer for this case, please? – vak Jun 28 '17 at 12:21