图的算子(属性算子,结构算子,join算子,计算用户粉丝数量,joinVertices和outerJoinVertices的区别)
一.图的算子
1.属性算子
类似于RDD的map操作
class Graph[VD, ED] {
def mapVertices[VD2](map: (VertexId, VD) => VD2): Graph[VD2, ED]
def mapEdges[ED2](map: Edge[ED] => ED2): Graph[VD, ED2]
def mapTriplets[ED2](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2]
}
(1)mapVertices
遍历所有的顶点(改变的是顶点的属性)
//生成新的graph:(vertextId,(name,age))=>(vartextId,(vartextId,name))
//方法一
scala> val t1_graph=userCallGraph.mapVertices{
case(vertextId,(name,age))=>(vertextId,name)}
t1_graph: org.apache.spark.graphx.Graph[(org.apache.spark.graphx.VertexId, String),Int] = org.apache.spark.graphx.impl.GraphImpl@527afebf
scala> t1_graph.vertices.collect.foreach(println)
(4,(4,David))
(1,(1,Alice))
(6,(6,Fran))
(3,(3,Charlie))
(5,(5,Ed))
(2,(2,Bob))
//方法二
scala> val t2_graph=userCallGraph.mapVertices((id,attr)=>(id,attr._1))
t2_graph: org.apache.spark.graphx.Graph[(org.apache.spark.graphx.VertexId, String),Int] = org.apache.spark.graphx.impl.GraphImpl@7c1958de
scala> t2_graph.vertices.collect.foreach(println)
(4,(4,David))
(1,(1,Alice))
(6,(6,Fran))
(3,(3,Charlie))
(5,(5,Ed))
(2,(2,Bob))
(2)mapEdges
遍历所有的边(改变的是边的属性)
//生成新的graph((scrId,srcAttr),(dstId,dstAttr),attr)=>(scrId,dstId,attr),生成新的图
scala> val t3_graph=userCallGraph.mapEdges(e=>Edge(e.srcId,e.dstId,e.attr*7.0))
t3_graph: org.apache.spark.graphx.Graph[(String, Int),org.apache.spark.graphx.Edge[Double]] = org.apache.spark.graphx.impl.GraphImpl@4777360f
scala> t3_graph.edges.collect.foreach(println)
Edge(2,1,Edge(2,1,49.0))
Edge(2,4,Edge(2,4,14.0))
Edge(3,2,Edge(3,2,28.0))
Edge(3,6,Edge(3,6,21.0))
Edge(4,1,Edge(4,1,7.0))
Edge(5,2,Edge(5,2,14.0))
Edge(5,3,Edge(5,3,56.0))
Edge(5,6,Edge(5,6,21.0))
(3)mapTriplets
遍历所有的三元组(改变的是第三个元素)
//将srcId+10,attr*10生成新的图
scala> val t3_graph=userCallGraph.mapTriplets(x=>(x.srcId+10,x.attr*10))
t3_graph: org.apache.spark.graphx.Graph[(String, Int),(Long, Int)] = org.apache.spark.graphx.impl.GraphImpl@4f88d48
scala> t3_graph.triplets.collect.foreach(println)
((2,(Bob,27)),(1,(Alice,28)),(12,70))
((2,(Bob,27)),(4,(David,42)),(12,20))
((3,(Charlie,65)),(2,(Bob,27)),