GraphX Tutorial
GraphX Tutorial
Sandbox.
GraphX is the Apache Spark component for graph-parallel computations, built upon a branch of
mathematics called graph theory. It is a distributed graph processing framework that sits on top of Spark
core.
A graph is a mathematical structure used to model relations between objects. A graph is made up of
vertices and edges that connect them. The vertices are the objects and the edges are the relationships
between them.
A directed graph is a graph where the edges have a direction associated with them. An example of a
directed graph is a Twitter follower. User Bob can follow user Carol without implying that user Carol
follows user Bob.
A regular graph is a graph where each vertex has the same number of edges. An example of a regular
graphs is Facebook friends. If Bob is a friend of Carol, then Carol is also a friend of Bob.
GraphX extends the Spark RDD with a Resilient Distributed Property Graph.
Build a Standalone Spark Application
The property graph is a directed multigraph which can have multiple edges in parallel. Every edge and
vertex has user defined properties associated with it. The parallel edges allow multiple relationships
between the same vertices.
Scenario
As a starting simple example, we will analyze 3 flights, for each flight we have the following information:
In this scenario, we are going to represent the airports as vertices and routes as edges. For our graph we
will have three vertices, each representing an airport. The Vertices each have an Id and the airport code
as a property:
The Edges have the Source Id , the Destination Id and the distance as a property.
1 2 1800
2 3 800
3 1 1400
This tutorial will run on the MapR Sandbox, which includes Spark.
You can download the code and data to run these examples from here:
o https://github.com/caroljmcdonald/sparkgraphxexample
The examples in this post can be run in the spark-shell, after launching with the spark-shell
command.
You can also run the code as a standalone application as described in the tutorial on Getting Started
with Spark on MapR Sandbox.
Log into the MapR Sandbox, as explained in Getting Started with Spark on MapR Sandbox, using userid
user01, password mapr. Start the spark shell with:
$ spark-shell
Define Vertices
First we will import the GraphX packages.
(In the code boxes, comments are in Green and output is in Blue)
import org.apache.spark._
import org.apache.spark.rdd.RDD
// import classes required for using GraphX
import org.apache.spark.graphx._
We define airports as vertices. Vertices have an Id and can have properties or attributes associated with
them. Each vertex consists of :
Vertex id Id (Long)
ID Property(V)
1 SFO
We define an RDD with the above properties that is then used for the Vertexes .
Define Edges
Edges are the routes between airports. An edge must have a source, a destination, and can have
properties. In our example, an edge consists of :
We define an RDD with the above properties that is then used for the Edges . The edge RDD has the
form (src id, dest id, distance ).
eRDD.take(2)
// Array(Edge(1,2,1800), Edge(2,3,800))
// graph edges
graph.edges.collect.foreach(println)
// Edge(1,2,1800)
// Edge(2,3,800)
// Edge(3,1,1400)
4. The EdgeTriplet class extends the Edge class by adding the srcAttr and dstAttr members which
contain the source and destination properties respectively.
// triplets
graph.triplets.take(3).foreach(println)
((1,SFO),(2,ORD),1800)
((2,ORD),(3,DFW),800)
((3,DFW),(1,SFO),1400)
In this scenario, we are going to represent the airports as vertices and routes as edges. We are interested
in visualizing airports and routes and would like to see the number of airports that have departures or
arrivals.
You can download the code and data to run these examples from here:
https://github.com/caroljmcdonald/sparkgraphxexample
Log into the MapR Sandbox, as explained in Getting Started with Spark on MapR Sandbox, using userid
user01, password mapr. Copy the sample data files to your sandbox home directory /user/user01 using
scp. Start the spark shell with:
$ spark-shell
Define Vertices
First we will import the GraphX packages.
import org.apache.spark._
import org.apache.spark.rdd.RDD
import org.apache.spark.util.IntParam
// import classes required for using GraphX
import org.apache.spark.graphx._
import org.apache.spark.graphx.util.GraphGenerators
Below we a Scala case classes to define the Flight schema corresponding to the csv data file.
The function below parses a line from the data file into the Flight class.
Below we load the data from the csv file into a Resilient Distributed Dataset (RDD). RDDs can have
transformations and actions, the first() action returns the first element in the RDD.
We define airports as vertices. Vertices can have properties or attributes associated with them. Each vertex
has the following property:
ID Property(V)
10397 ATL
We define an RDD with the above properties that is then used for the Vertexes .
airports.take(1)
// Array((14057,PDX))
Define Edges
Edges are the routes between airports. An edge must have a source, a destination, and can have
properties. In our example, an edge consists of :
routes.take(2)
// Array(((14869,14683),1087), ((14683,14771),1482))
edges.take(1)
//Array(Edge(10299,10926,160))
// graph vertices
graph.vertices.take(2)
Array((10208,AGS), (10268,ALO))
// graph edges
graph.edges.take(2)
Array(Edge(10135,10397,692), Edge(10135,13930,654))
9. The EdgeTriplet class extends the Edge class by adding the srcAttr and dstAttr members which
contain the source and destination properties respectively.
// triplets
graph.triplets.take(3).foreach(println)
((10135,ABE),(10397,ATL),692)
((10135,ABE),(13930,ORD),654)
((10140,ABQ),(10397,ATL),1269)
airportMap(10397)
res70: String = ATL
maxIncoming.foreach(println)
(ATL,152)
(ORD,145)
(DFW,143)
maxout.foreach(println)
(10397,(153,ATL))
(13930,(146,ORD))
(11298,(143,DFW))
PageRank
Another GraphX operator is PageRank. which is based on the Google PageRank algorithm.
PageRank measures the importance of each vertex in a graph, by determining which vertexes have the
most edges with other vertexes. In our example we can use PageRank to determine which airports are
the most important, by measuring which airports have the most connections to other airports.
We have to specify the tolerance, which is the measure of convergence.
// use pageRank
val ranks = graph.pageRank(0.1).vertices
Pregel
Many important graph algorithms are iterative algorithms since properties of vertices depend on
properties of their neighbors, which depend on properties of their neighbors. Pregel is an iterative graph
processing model, developed at Google, which uses a sequence of iterations of message passing
between vertices in a graph. GraphX Implements a Pregel-like bulk-synchronous message-passing API.
With the Pregel implementation in GraphX vertices can only send messages to neighboring vertices.
The Pregel operator is executed in a series of super steps. In each super step:
the vertices receive the sum of their inbound messages from the previous super step,
When there are no more messages remaining, the Pregel operator will end the iteration and the final
graph is returned.
The code below computes the cheapest airfare using Pregel with the following formula to compute airfare.
50 + distance / 20
// starting vertex
val sourceId: VertexId = 13024
// a graph with edges containing airfare cost calculation
val gg = graph.mapEdges(e => 50.toDouble + e.attr.toDouble/20 )
// initialize graph, all vertices except source have distance infinity
val initialGraph = gg.mapVertices((id, _) => if (id == sourceId) 0.0 else Double.PositiveInfinity)
// call pregel on graph
val sssp = initialGraph.pregel(Double.PositiveInfinity)(
// Vertex Program
(id, dist, newDist) => math.min(dist, newDist),
triplet => {
// Send Message
(10208,277.79)
(10268,260.7)
(14828,261.65)
(14698,125.25)