spark api学习之GraphOps类

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表示统计节点的入度方向的邻居,所以对于图的左边节点的入度方向邻居数为0

    扫描二维码关注公众号,回复: 4232199 查看本文章
     graph.collectNeighbors(EdgeDirection.In).foreach(println(_))
    

    result:
    在这里插入图片描述
    2.edgeDirection.Out表示统计节点的出度方向的邻居,所以对于图的右边节点的出度方向邻居数为0
    result:
    在这里插入图片描述
    3.edgeDirection.Either表示节点的出度和入度方向的邻居都会统计
    result:
    在这里插入图片描述

  • collectNeighborIds(edgeDirection: EdgeDirection): VertexRDD[Array[VertexId]]
    collectNeighborIds是将每一个节点的所有邻居节点的vertexId聚合成一个数组

  • collectEdges

猜你喜欢

转载自blog.csdn.net/qq_27696709/article/details/83118365