Graph ~3:出入度等信息展示

Graph 出入度等信息展示

numEdges                             边数量
numVertices                           顶点数量
inDegrees: VertexRDD            入度
outDegrees: VertexRDD         出度
degrees: VertexRDD               度

一:spark-shell

查看边的数量

scala> graph3.numEdges
res16: Long = 8

查看顶点数量

scala> graph3.numVertices
res17: Long = 7

查看入度

scala> graph3.inDegrees.collect
res8: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((4,1), (8,1), (1,3), (6,1), (7,2))

查看出度

scala> graph3.outDegrees.collect
res9: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((1,1), (6,1), (2,4), (3,1), (7,1))

查看所有的度

scala> graph3.degrees.collect
res10: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((4,1), (8,1), (1,4), (6,2), (2,4), (3,1), (7,3))

二:JAVA API

reverse主要是将边的方向反向

/**
 * @author xubo
 * ref http://spark.apache.org/docs/1.5.2/graphx-programming-guide.html
 * time 20160503
 */

package org.apache.spark.graphx.learning

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.graphx.Graph
import org.apache.spark.graphx.VertexRDD
import org.apache.spark.graphx.util.GraphGenerators

object GraphGeneratorsAndIndegreeOutdegree {

  def main(args: Array[String]): Unit = {
  
    val conf = new SparkConf().setAppName("GraphOperatorsStructuralMask").setMaster("local[4]")
  
    val sc = new SparkContext(conf)

    // 导入随机图生成库
    // 创建一个顶点属性为“age”的图。为了简单起见,这里我们使用随机图。
    
    val graph: Graph[Double, Int] =
      GraphGenerators.logNormalGraph(sc, numVertices = 5).mapVertices((id, _) => id.toDouble)
      
    // 计算年长追随者的数量和他们的总年龄

	### Graph
    println("sc.defaultParallelism:" + sc.defaultParallelism);
    
    ### vertices
    graph.vertices.collect.foreach(println(_))
    
    ### edges
    graph.edges.collect.foreach(println(_))
    println("count:" + graph.edges.count);
    
    ### inDegrees
    graph.inDegrees.foreach(println)
    
    ### outDegrees
    graph.outDegrees.foreach(println)
    
    ### reverse
    ### reverse vertices
    graph.reverse.vertices.collect.foreach(println)
    
    ### reverse edges
    graph.reverse.edges.collect.foreach(println)
    
    ### reverse inDegrees
    graph.reverse.inDegrees.foreach(println)
    
    ### reverse inDegrees
    graph.reverse.outDegrees.foreach(println)
  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值