GraphOps类是图计算领域spark graphx的图操作类,Graph类实例也能用GraphOps的方法,因为如果Graph类实例要用到GraphOps的方法时jvm会自动将Graph类型隐式转换成GraphOps类型
来不及解释了,先建个图
//定义样本数据
val rdd = sparkSession.sparkContext.makeRDD(Array(
(1, 18), (2, 14), (3, 12), (4, 11),
(5, 14), (6, 17), (5, 13),
(8, 19), (9, 12), (6, 18)
))
//生成边
val edge = rdd.map(line => {
Edge(line._1, line._2, 1L)
})
//通过边构建图(有向二分图)
val graph = Graph.fromEdges(edge, 0)
生成的图如下:
-
numEdges:Long
返回图的边数量graph.numEdges //10
-
numVertices:Long
返回图的节点数量graph.numVertices //15
-
inDegrees:VertexRDD[Int]
计算图的入度,入度是指指向图右边每个节点的边数。返回的类型是VertexRDD,rdd每一行数据类型是(VertexId, Int)。graph.inDegrees.foreach(println(_))
result:
-
outDegrees:VertexRDD[Int]
计算图的出度,出度是指从图左边每个节点出发的边数。返回的类型是VertexRDD,rdd每一行数据类型是(VertexId, Int)graph.outDegrees.foreach(println(_))
result:
-
degrees:VertexRDD[Int]
计算图的出度与入度graph.degrees.foreach(println(_))
result:
-
collectNeighborIds(edgeDirection: EdgeDirection):VertexRDD[Array[VertexId]]
将每一个节点的所有邻居节点的vertexId和属性值聚合成一个数组。
对于参数edgeDirection,有如下值:
1.edgeDirection.In表示统计节点的入度方向的邻居,所以对于图的左边节点的入度方向邻居数为0graph.collectNeighbors(EdgeDirection.In).foreach(println(_))
result:
2.edgeDirection.Out表示统计节点的出度方向的邻居,所以对于图的右边节点的出度方向邻居数为0
result:
3.edgeDirection.Either表示节点的出度和入度方向的邻居都会统计
result:
-
collectNeighborIds(edgeDirection: EdgeDirection): VertexRDD[Array[VertexId]]
collectNeighborIds是将每一个节点的所有邻居节点的vertexId聚合成一个数组 -
collectEdges