学习GraphX
首先準備如下社交圖形數據:
打開spark-shell;
導入相關包:
import org.apache.spark._
import org.apache.spark.graphx._
import org.apache.spark.rdd.RDD創建如上graph對象:
// Create an RDD for the vertices
val users: RDD[(VertexId, (String, Boolean))] =sc.parallelize(Array((1L, ("Li Yapeng", true)), (2L, ("Wang Fei", false)), (3L, ("Xie Tingfeng", true)), (4L, ("Zhang Bozhi", false)), (5L, ("Chen Guanxi", true))))
// Create an RDD for edges
val relationships: RDD[Edge[String]] =sc.parallelize(Array(Edge(1L, 2L, "spouse"), Edge(2L, 3L, "spouse"),Edge(3L, 4L, "spouse"), Edge(4L, 5L, "friend"), Edge(3L, 5L, "friend")))
// Define a default user in case there are relationship with missing user
val defaultUser = ("Who?", false)
// Build the initial Graph
val graph = Graph(users, relationships, defaultUser)嘗試打印出所有的男藝人:
graph.vertices.filter(_._2._2).collectres8: Array[(org.apache.spark.graphx.VertexId, (String, Boolean))] = Array((1,(Li Yapeng,true)), (3,(Xie Tingfeng,true)), (5,(Chen Guanxi,true)))按焦點程度逆序打印出藝人的ID:
graph.degrees.sortBy(_._2,false).collectres12: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((3,3), (2,2), (4,2), (5,2), (1,1))但是沒辦法知道藝人的名字,join一下:
graph.degrees.leftJoin(graph.vertices)((vid, vd1, vd2)=>(vd2.get._1,vd1)).sortBy(_._2._2, false).collectres35: Array[(org.apache.spark.graphx.VertexId, (String, Int))] = Array((3,(Xie Tingfeng,3)), (2,(Wang Fei,2)), (4,(Zhang Bozhi,2)), (5,(Chen Guanxi,2)), (1,(Li Yapeng,1)))直接拿vd2.get有點冒險,下面改成安全版本:
graph.degrees.leftJoin(graph.vertices)((vid, vd1, vd2)=>(vd2 match {case Some(vvd2) => (vvd2._1, vd1); case None => ("", vd1)})).sortBy(_._2._2, false).collect測試一下消息機制,發送配偶信息給每個人:
graph.aggregateMessages({
(ctx:EdgeContext[(String, Boolean),String,String])=>
if(ctx.attr=="spouse"){
ctx.sendToSrc(ctx.dstAttr._1);
ctx.sendToDst(ctx.srcAttr._1)
}
}, ((s1:String,s2:String)=>s1+"|"+s2)).collectres46: Array[(org.apache.spark.graphx.VertexId, String)] = Array((1,Wang Fei), (2,Li Yapeng|Xie Tingfeng), (3,Wang Fei|Zhang Bozhi), (4,Xie Tingfeng))輸出pagerank值:
import org.apache.spark.graphx.lib
graph.pageRank(0.01).vertices.collect.foreach(println)(1,0.15)
(2,0.27749999999999997)
(3,0.38587499999999997)
(4,0.313996875)
(5,0.58089421875)數一數每個藝人所處的三角關系:
graph.triangleCount.vertices.collectres48: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((1,0), (2,0), (3,1), (4,1), (5,1))轉載于:https://www.cnblogs.com/bluejoe/p/5115846.html
總結
- 上一篇: 微信网名男四个字的
- 下一篇: BZOJ-1005 明明的烦恼