GraphX是Spark用于图形并行计算的新组件。在较高的层次上,GraphX通过引入一个新的Graph抽象来扩展Spark RDD:一个定向的多图,其属性附加到每个定点和边。为了支持图计算,GraphX公开了一组基本的操作符(子图,joinVertices和aggregateMessages),以及上述优化的变体API。
1.导入
2.属性图
GraphX的属性曲线图是一个有向多重图与连接到每个顶点边缘的用户定义对象。其可能有多个平行边共享相同的源和目标顶点。支持平行边缘的能力简化了在相同顶点之间存在多个关系的建模场景(例如:即使朋友又是同事)。每个顶点都是由唯一的64位长标识符的键控制。GraphX不对顶点标识符施加任何排序约束。
与RDD一样,属性图是不可变,分布式和容错的。通过生成具有所需更改的新图来完成对图的值或结构的修改。原始图形的大部分(未影响的结构,属性和索引)在新图中重复使用,从而降低了此固有功能数据结构的成本。
分别使用graph.vertices和graph.edges成员将图解析成相应的顶点和边视图。graph.vertices返回一个VertexRDD扩展的RDD,graph.edges返回包含Edge的对象。
3.图形运算符
正如RDDS有基本操作map,filter和reduceByKey等一样,图也有类似的函数,产生具有转化特性和结构的新图。已定义的具有优化实现的核心运算符如下:


1 val numEdges: Long 2 val numVertices: Long 3 val inDegrees: VertexRDD[Int] 4 val outDegrees: VertexRDD[Int] 5 val degrees: VertexRDD[Int] 6 // Views of the graph as collections 7 val vertices: VertexRDD[VD] 8 val edges: EdgeRDD[ED] 9 val triplets: RDD[EdgeTriplet[VD, ED]] 10 // Functions for caching graphs 11 def persist(newLevel: StorageLevel = StorageLevel.MEMORY_ONLY): Graph[VD, ED] 12 def cache(): Graph[VD, ED] 13 def unpersistVertices(blocking: Boolean = true): Graph[VD, ED] 14 // Change the partitioning heuristic 15 def partitionBy(partitionStrategy: PartitionStrategy): Graph[VD, ED] 16 // Transform vertex and edge attributes 17 def mapVertices[VD2](map: (VertexId, VD) => VD2): Graph[VD2, ED] 18 def mapEdges[ED2](map: Edge[ED] => ED2): Graph[VD, ED2] 19 def mapEdges[ED2](map: (PartitionID, Iterator[Edge[ED]]) => Iterator[ED2]): Graph[VD, ED2] 20 def mapTriplets[ED2](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2] 21 def mapTriplets[ED2](map: (PartitionID, Iterator[EdgeTriplet[VD, ED]]) => Iterator[ED2]) 22 : Graph[VD, ED2] 23 // Modify the graph structure 24 def reverse: Graph[VD, ED] 25 def subgraph( 26 epred: EdgeTriplet[VD,ED] => Boolean = (x => true), 27 vpred: (VertexId, VD) => Boolean = ((v, d) => true)) 28 : Graph[VD, ED] 29 def mask[VD2, ED2](other: Graph[VD2, ED2]): Graph[VD, ED] 30 def groupEdges(merge: (ED, ED) => ED): Graph[VD, ED] 31 // Join RDDs with the graph 32 def joinVertices[U](table: RDD[(VertexId, U)])(mapFunc: (VertexId, VD, U) => VD): Graph[VD, ED] 33 def outerJoinVertices[U, VD2](other: RDD[(VertexId, U)]) 34 (mapFunc: (VertexId, VD, Option[U]) => VD2) 35 : Graph[VD2, ED] 36 // Aggregate information about adjacent triplets 37 def collectNeighborIds(edgeDirection: EdgeDirection): VertexRDD[Array[VertexId]] 38 def collectNeighbors(edgeDirection: EdgeDirection): VertexRDD[Array[(VertexId, VD)]] 39 def aggregateMessages[Msg: ClassTag]( 40 sendMsg: EdgeContext[VD, ED, Msg] => Unit, 41 mergeMsg: (Msg, Msg) => Msg, 42 tripletFields: TripletFields = TripletFields.All) 43 : VertexRDD[A] 44 // Iterative graph-parallel computation 45 def pregel[A](initialMsg: A, maxIterations: Int, activeDirection: EdgeDirection)( 46 vprog: (VertexId, VD, A) => VD, 47 sendMsg: EdgeTriplet[VD, ED] => Iterator[(VertexId,A)], 48 mergeMsg: (A, A) => A) 49 : Graph[VD, ED] 50 // Basic graph algorithms 51 def pageRank(tol: Double, resetProb: Double = 0.15): Graph[Double, Double] 52 def connectedComponents(): Graph[VertexId, ED] 53 def triangleCount(): Graph[Int, ED] 54 def stronglyConnectedComponents(numIter: Int): Graph[VertexId, ED]
这些运算符中的每一个都生成一个新图形,其顶点或边缘的属性由用户定义的map函数修改。
注意:在每种情况下,图形的结构都不会改变,这是这些运算符的关键特征,它允许结果图重用原始图的结构索引!
4.结构运算符
1 class Graph[VD, ED] { 2 def reverse: Graph[VD, ED] 3 def subgraph(epred: EdgeTriplet[VD,ED] => Boolean, 4 vpred: (VertexId, VD) => Boolean): Graph[VD, ED] 5 def mask[VD2, ED2](other: Graph[VD2, ED2]): Graph[VD, ED] 6 def groupEdges(merge: (ED, ED) => ED): Graph[VD,ED] 7 }
函数reverse将返回逆转所有边缘方向的新图。当尝试计算反向PageRank时,这可能会很有用。由于反向操作不会修改顶点或边缘属性或更改边数,因此可以有效的实现,而无需数据移动或复制。
在许多情况下,有必要将外部数据(RDD)与图形关联起来。
1 class Graph[VD, ED] { 2 def joinVertices[U](table: RDD[(VertexId, U)])(map: (VertexId, VD, U) => VD) 3 : Graph[VD, ED] 4 def outerJoinVertices[U, VD2](table: RDD[(VertexId, U)])(map: (VertexId, VD, Option[U]) => VD2) 5 : Graph[VD2, ED] 6 }
内连接(joinVertices)运算符连接输入RDD并返回通过用户定义关系关联的新图形。RDD中没有匹配的顶点保留原始值。
外连接(outerJoinVertices)运算符:因为并非所有的顶点在输入RDD中都具有匹配值,所以在不确定的时候可以采用该类型。