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)
}
}