Original link: Graphx in Pregel single source point shortest path
Example of a single source point shortest path in Graphx, using the method of class Pregel.
The core part is three functions:
1. Node processing message function Vprog: (Vertexid, VD, A) = VD (Node ID, node attribute, message) = = Node Property
2. Node Send Message function SENDMSG:EDGETRIPLET[VD, ED] = iterator[(vertexid,a)] (side tuple) + iterator[(target Node ID, message)]
3. Message merge function Mergemsg: (A, a) + a) (message, message) = = Message
PackageMyClass. GraphXImportorg.apache.spark.graphx._ImportOrg.apache.spark.SparkContext//Import random graph Generation Library Importorg.apache.spark.graphx.util.GraphGenerators/*** Created by Jack on 3/4/14. */Object Pregel {def main (args:array[string]) {val sc=NewSparkcontext ("local", "Pregel test", System.getenv ("Spark_home"), Sparkcontext.jarofclass ( This. GetClass)) //A graph with edge attributes containing distances//Initializes a random graph in which the degree of the node conforms to the logarithmic normal distribution and the Edge property is initialized to 1Val graph:graph[int, Double] =Graphgenerators.lognormalgraph (SC, numvertices= ten). Mapedges (E =e.attr.todouble) Graph.edges.foreach (println) Val Sourceid:vertexid= 4//The Ultimate Source//Initialize The graph such that all vertices except the root has distance infinity. //initialize the distance from each node to the origin pointVal initialgraph = graph.mapvertices (id, _) = =if(id = = SourceID) 0.0Elsedouble.positiveinfinity) Val sssp=Initialgraph.pregel (double.positiveinfinity) (//Vertex Program, a function of node processing messages, dist as the original Node property (double), newdist as the message type (double)(ID, dist, newdist) =math.min (Dist, newdist),//send Message, which returns the result as (target Node ID, message (i.e. shortest distance))triplet = { if(Triplet.srcattr + triplet.attr <triplet.dstattr) {Iterator (Triplet.dstid, triplet.srcattr+triplet.attr))}Else{Iterator.empty}},//Merge message, merging messages, similar to combiner in Hadoop(A, b) = =Math.min (A, B)) println (sssp.vertices.collect.mkString ("\ n")) } }
Additional Reference Links:
http://bbs.csdn.net/topics/390985424
Graphx Pregel Single source point shortest path (RPM)