# Spark组件之GraphX学习6--随机图生成和出度入度等信息显示

1解释

2.代码：

```/**
* @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 = {
// Assume the SparkContext has already been constructed
val sc = new SparkContext(conf)

// Import random graph generation library
// Create a graph with "age" as the vertex property.  Here we use a random graph for simplicity.
val graph: Graph[Double, Int] =
GraphGenerators.logNormalGraph(sc, numVertices = 5).mapVertices((id, _) => id.toDouble)
// Compute the number of older followers and their total age

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

}```

reverse主要是将边的方向反向

3.结果：

```Graph:
sc.defaultParallelism:4
vertices:
(4,4.0)
(0,0.0)
(1,1.0)
(2,2.0)
(3,3.0)
edges:
Edge(0,0,1)
Edge(0,2,1)
Edge(0,2,1)
Edge(0,3,1)
Edge(1,3,1)
Edge(1,4,1)
Edge(1,4,1)
Edge(1,4,1)
Edge(2,0,1)
Edge(2,3,1)
Edge(2,3,1)
Edge(2,4,1)
Edge(3,1,1)
Edge(3,3,1)
Edge(4,0,1)
Edge(4,1,1)
Edge(4,2,1)
Edge(4,4,1)
count:18

inDegrees
(3,5)
(1,2)
(4,5)
(0,3)
(2,3)

outDegrees
(2,4)
(3,2)
(1,4)
(4,4)
(0,4)

reverse

reverse vertices
(4,4.0)
(0,0.0)
(1,1.0)
(2,2.0)
(3,3.0)

reverse edges
Edge(0,0,1)
Edge(2,0,1)
Edge(2,0,1)
Edge(3,0,1)
Edge(3,1,1)
Edge(4,1,1)
Edge(4,1,1)
Edge(4,1,1)
Edge(0,2,1)
Edge(3,2,1)
Edge(3,2,1)
Edge(4,2,1)
Edge(0,4,1)
Edge(1,3,1)
Edge(1,4,1)
Edge(2,4,1)
Edge(3,3,1)
Edge(4,4,1)

reverse inDegrees
(4,4)
(0,4)
(2,4)
(1,4)
(3,2)

reverse inDegrees
(2,3)
(1,2)
(3,5)
(4,5)
(0,3)```

【1】 http://spark.apache.org/docs/1.5.2/graphx-programming-guide.html

【2】https://github.com/xubo245/SparkLearning