Apache Spark GraphX Describes Organizational Chart Easy
George Jen, Jen Tek LLC
GraphX is a component in Spark for graphs and graph-parallel computation. At a high level, GraphX extends the Spark RDD by introducing a new Graph abstraction: a directed multigraph with properties attached to each vertex and edge.
GraphX includes a growing collection of graph algorithms and builders to simplify graph analytics tasks.
Here what I want to demonstrate is to use Graphx to describe an organizational chart, a most fundamental use case of graph computing.
Example:
Let’s define a simple org chart:
Let’s add one more person, sherry, Jack’s wife
First, I try it on a SQL database. I have PostgreSQL on my machine, therefore, use it:
Create 2 tables:
Vertex:
stores the id, name, title formation. Vertex stores the box or node
Edge:
Stores source id, destination id and relationship, Edge stores the line, or relationship
create table vertex
(id bigint,
property_name text,
property_title text
);
create table edge
(
src_id bigint,
dest_id bigint,
relationship text
);
Then add data into Vertex and Edge tables
The box:
Sherry, wife of owner
Jack, owner
George, clerk
Mary, sales
The line, indicates the relationship in the org chart:
Jack, owner is boss of George, clerk
Jack, owner is boss of Mary, sales
Sherry, wife of owner is boss of Jack, owner
George, clerk is coworker of Mary, sales
insert into vertex values (1,’Jack’,’owner’),(2, ‘George’, ‘clerk’), (3, ‘Mary’, ‘Sales’), (4, ‘Shrry’, ‘wife of owner’);
insert into edge values (1,2,’boss’), (1,3,’boss’), (2,3,’coworker’),(4,1,’boss’);
commit;
with x as (SELECT e.src_id, e.dest_id, e.relationship,
src.property_name src_name,
src.property_title src_title,
dst.property_name dest_name,
dst.property_title dest_title
FROM edge AS e LEFT JOIN vertex AS src ON e.src_id = src.id
LEFT JOIN vertex AS dst ON e.dest_id = dst.id)
select src_name || ‘, ‘ || src_title || ‘, is ‘
|| relationship || ‘ of ‘ || dest_name || ‘, ‘
|| dest_title description from x;
description
— — — — — — — — — — — — — — — — — — — — — — —
shrry, wife of owner, is boss of Jack, owner
Jack, owner, is boss of George, clerk
Jack, owner, is boss of Mary, Sales
George, clerk, is coworker of Mary, Sales
(4 rows)
Now switch to Spark GraphX with Scala code below:
import org.apache.spark._
import org.apache.spark.graphx._
import org.apache.spark.rdd.RDD
import org.apache.log4j._
import org.apache.spark.sql._
import org.apache.spark.graphx.{Graph, VertexRDD}
import org.apache.spark.graphx.util.GraphGenerators
Logger.getLogger(“org”).setLevel(Level.ERROR)
val spark = SparkSession
.builder
.appName(“graphx”)
.master(“local[*]”)
.config(“spark.sql.warehouse.dir”, “file:///tmp”)
.getOrCreate()
val sc=spark.sparkContext
val users: RDD[(VertexId, (String, String))] =
sc.parallelize(Array((1L, (“jack”, “owner”)), (2L, (“george”, “clerk”)),
(3L, (“mary”, “sales”)), (4L, (“sherry”, “owner wife”))))
val relationships: RDD[Edge[String]] =
sc.parallelize(Array(Edge(1L, 2L, “boss”), Edge(1L, 3L, “boss”),
Edge(2L, 3L, “coworker”), Edge(4L, 1L, “boss”)))
val defaultUser = (“”, “Missing”)
val graph = Graph(users, relationships, defaultUser)
val facts: RDD[String] =
graph.triplets.map(triplet =>
triplet.srcAttr._1 + “, “+ triplet.srcAttr._2 + “ is the “ + triplet.attr + “ of “ + triplet.dstAttr._1+”, “+triplet.dstAttr._2)
facts.collect.foreach(println(_))
Run the above scala code produces output below:
jack, owner is the boss of george, clerk
jack, owner is the boss of mary, sales
george, clerk is the coworker of mary, sales
sherry, owner wife is the boss of jack, owner
facts: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[20] at map at <console>:43
Summary:
If this is a complex chart, Spark Graphx is the perfect platform to compute the inter-relationships because of its distributed, in memory computing nature and rich GraphX computing library
The code used in this writing is in my github site: