Guest User

Untitled

a guest
Apr 16th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.05 KB | None | 0 0
  1. import org.apache.spark.{SparkContext, SparkConf}
  2. import org.apache.spark.graphx._
  3. import org.apache.spark.rdd.RDD
  4.  
  5. object TopPaths {
  6.  
  7.   def main(args: Array[String]) {
  8.     val sc = new SparkContext(new SparkConf().setMaster("local[1]").setAppName("Hits"))
  9.  
  10.     val vertices: RDD[(VertexId, String)] = sc.parallelize(Array(
  11.       (1l, "G"), (2l, "S"), (3l, "C"),
  12.       (4l, "G"),
  13.       (5l, "D"), (6l, "D"), (7l, "G"), (8l, "C"),
  14.       (9l, "S"), (10l, "C"), (11l, "D"),
  15.       (12l, "G"), (13l, "S"),
  16.       (14l, "C")))
  17.  
  18.     val edges: RDD[Edge[String]] = sc.parallelize(Array(
  19.       Edge(1l, 2l, ""), Edge(2l, 3l, ""),
  20.       Edge(5l, 6l, ""), Edge(6l, 7l, ""), Edge(7l, 8l, ""),
  21.       Edge(9l, 10l, ""), Edge(10l, 11l, ""),
  22.       Edge(12l, 13l, "")
  23.     ))
  24.  
  25.     val graph = Graph(vertices, edges).reverse.
  26.       mapVertices((id, pageType) => (pageType, List(pageType)))
  27. //    val graph = Graph(vertices, edges).
  28. //      mapVertices((id, pageType) => (pageType, List(pageType)))
  29.  
  30.     val pathGraph = Pregel(graph, List.empty[String],
  31.       maxIterations = 4,  activeDirection = EdgeDirection.Out)(
  32.         vertexProgram, sendMessage, combineMessages)
  33.  
  34.     pathGraph.vertices.collect().foreach(println(_))
  35.   }
  36.  
  37.   // append the received path the this vertex attribute
  38.   def vertexProgram(id: VertexId, attr: (String, List[String]), msg: List[String]) = {
  39.     if (msg.nonEmpty) (attr._1, attr._2 ::: msg) else attr
  40.   }
  41.  
  42.   // propagate the path to outer vertices
  43.   def sendMessage(edge: EdgeTriplet[(String, List[String]), String]) = {
  44.     val pageType = edge.srcAttr._1
  45.     val path = edge.srcAttr._2
  46.  
  47.     if (pageType == "C" && path.size == 1) {
  48.       println("Sending initial message from " + edge)
  49.       Iterator((edge.dstId, path))
  50.     } else if (path.size > 1) {
  51.       println("Sending message of size " + path.size + " to " + edge.dstId)
  52.       Iterator((edge.dstId, path))
  53.     } else {
  54.       println("No more messages from " + edge)
  55.       Iterator.empty
  56.     }
  57.   }
  58.  
  59.   def combineMessages(msg1: List[String], msg2: List[String]) = { msg1 }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment