Spark學習筆記:joinVertices & outJoinVertices

joinVertices
若graph頂點的Id在點集pointTest中,則將graph頂點的屬性使替換爲點集pointTest的點屬性;
若graph頂點Id不在點集pointTest中,則不對graph頂點屬性做任何修改。

1.創建graph

scala> val graph = GraphLoader.edgeListFile(sc, "file:///home/hadoop/1.txt")
scala> graph.triplets.collect.foreach(println(_))

這裏寫圖片描述


2.自定義pointTest點集

scala> val pointTest: RDD[(VertexId,Int)] = sc.parallelize(Array((1L,10),(2L,10),(3L,10)))

這裏寫圖片描述


3.將graph頂點屬性置爲零;joinVertices:

scala> val outputGraph = graph.mapVertices((id,attr) => 0).joinVertices(pointTest)((_,_,optDeg) => optDeg)
scala> outputGraph.triplets.collect.foreach(println(_))

這裏寫圖片描述


4.outJoinVertices

scala> val outputGraph = graph.mapVertices((id,attr) => 0).outerJoinVertices(pointTest)((_,_,optDeg) => optDeg.getOrElse(0))

—————————–

將頂點屬性改爲各點出度:

scala> val outputGraph = graph.mapVertices((id,attr) => 0).outerJoinVertices(graph.outDegrees)((_,_,optDeg) => optDeg.getOrElse(0))

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章