spark graphx

匿名 (未验证) 提交于 2019-12-03 00:30:01

在介绍完Spark GraphX的属性图模型、简单的属性展示操作后,本章节介绍更多有关Spark GraphX的常用图操作。

在GraphX中,核心操作都是被优化过的,组合核心操作的定义在GraphOps中。
由于Scala隐式转换,定义在GraphOps的操作可以在Graph的成员中获取。例如:我们计算图中每个顶点的入度.(该方法是定义在GraphOps)

  1. val graph: Graph[(String, String), String]
  2. // Use the implicit GraphOps.inDegrees operator
  3. val inDegrees: VertexRDD[Int] = graph.inDegrees
scala

下面我们列出常用的几个图操作

操作列表概述

这里只列出Graph中常用的操作函数API,仍有一些高级函数没有列出,如果需要还请参考Spark API文档。

  1. /** Summary of the functionality in the property graph */
  2. class Graph[VD, ED] {
  3. // Information about the Graph ===================================================================
  4. val numEdges: Long
  5. val numVertices: Long
  6. val inDegrees: VertexRDD[Int]
  7. val outDegrees: VertexRDD[Int]
  8. val degrees: VertexRDD[Int]
  9. // Views of the graph as collections =============================================================
  10. val vertices: VertexRDD[VD]
  11. val edges: EdgeRDD[ED]
  12. val triplets: RDD[EdgeTriplet[VD, ED]]
  13. // Functions for caching graphs ==================================================================
  14. def persist(newLevel: StorageLevel = StorageLevel.MEMORY_ONLY): Graph[VD, ED]
  15. def cache(): Graph[VD, ED]
  16. def unpersistVertices(blocking: Boolean = true): Graph[VD, ED]
  17. // Change the partitioning heuristic ============================================================
  18. def partitionBy(partitionStrategy: PartitionStrategy): Graph[VD, ED]
  19. // Transform vertex and edge attributes ==========================================================
  20. def mapVertices[VD2](map: (VertexId, VD) => VD2): Graph[VD2, ED]
  21. def mapEdges[ED2](map: Edge[ED] => ED2): Graph[VD, ED2]
  22. def mapEdges[ED2](map: (PartitionID, Iterator[Edge[ED]]) => Iterator[ED2]): Graph[VD, ED2]
  23. def mapTriplets[ED2](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2]
  24. def mapTriplets[ED2](map: (PartitionID, Iterator[EdgeTriplet[VD, ED]]) => Iterator[ED2])
  25. : Graph[VD, ED2]
  26. // Modify the graph structure ====================================================================
  27. def reverse: Graph[VD, ED]
  28. def subgraph(
  29. epred: EdgeTriplet[VD,ED] => Boolean = (x => true),
  30. vpred: (VertexId, VD) => Boolean = ((v, d) => true))
  31. : Graph[VD, ED]
  32. def mask[VD2, ED2](other: Graph[VD2, ED2]): Graph[VD, ED]
  33. def groupEdges(merge: (ED, ED) => ED): Graph[VD, ED]
  34. // Join RDDs with the graph ======================================================================
  35. def joinVertices[U](table: RDD[(VertexId, U)])(mapFunc: (VertexId, VD, U) => VD): Graph[VD, ED]
  36. def outerJoinVertices[U, VD2](other: RDD[(VertexId, U)])
  37. (mapFunc: (VertexId, VD, Option[U]) => VD2)
  38. : Graph[VD2, ED]
  39. // Aggregate information about adjacent triplets =================================================
  40. def collectNeighborIds(edgeDirection: EdgeDirection): VertexRDD[Array[VertexId]]
  41. def collectNeighbors(edgeDirection: EdgeDirection): VertexRDD[Array[(VertexId, VD)]]
  42. def aggregateMessages[Msg: ClassTag](
  43. sendMsg: EdgeContext[VD, ED, Msg] => Unit,
  44. mergeMsg: (Msg, Msg) => Msg,
  45. tripletFields: TripletFields = TripletFields.All)
  46. : VertexRDD[A]
  47. // Iterative graph-parallel computation ==========================================================
  48. def pregel[A](initialMsg: A, maxIterations: Int, activeDirection: EdgeDirection)(
  49. vprog: (VertexId, VD, A) => VD,
  50. sendMsg: EdgeTriplet[VD, ED] => Iterator[(VertexId,A)],
  51. mergeMsg: (A, A) => A)
  52. : Graph[VD, ED]
  53. // Basic graph algorithms ========================================================================
  54. def pageRank(tol: Double, resetProb: Double = 0.15): Graph[Double, Double]
  55. def connectedComponents(): Graph[VertexId, ED]
  56. def triangleCount(): Graph[Int, ED]
  57. def stronglyConnectedComponents(numIter: Int): Graph[VertexId, ED]
  58. }
scala

属性操作

属性图中包括类似RDD map的操作,如下图:

  1. class Graph[VD, ED] {
  2. def mapVertices[VD2](map: (VertexId, VD) => VD2): Graph[VD2, ED]
  3. def mapEdges[ED2](map: Edge[ED] => ED2): Graph[VD, ED2]
  4. def mapTriplets[ED2](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2]
  5. }
scala

mapVertices遍历所有的顶点,mapEdges遍历所有的边,mapTriplets遍历所有的三元组。

注意,属性操作下,图的结构都不受影响。这些操作的一个重要特征是它允许所得图形重用原有图形的结构索引(indices)。

属性操作常用来进行特殊计算或者排除不需要的属性.我们依旧以上一章节的图为例,进行操作。下面没有列出全部代码,包的导入和图的构建请参考上一章节的内容Spark2.1.0入门:Spark GraphX简介

  1. /***********************************
  2. *属性操作
  3. ***********************************/
  4. println("---------------------------------------------")
  5. println("给图中每个顶点的职业名的末尾加上'dblab'字符串")
  6. graph.mapVertices{ case (id, (name, occupation)) => (id, (name, occupation+"dblab"))}.vertices.collect.foreach(v => println(s"${v._2._1} is ${v._2._2}"))
  7. println("---------------------------------------------")
  8. println("给图中每个元组的Edge的属性值设置为源顶点属性值加上目标顶点属性值:")
  9. graph.mapTriplets(triplet => triplet.srcAttr._2 + triplet.attr + triplet.dstAttr._2).edges.collect.foreach(println(_))
Scala

结构操作

目前Spark GraphX只支持一些简单的常用结构操作,还在不断完善中。
常用的操作如下:

  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. }
scala

reverse操作返回一个所有边方向取反的新图.该反转操作并没有修改图中顶点、边的属性,更没有增加边的数量。
subgraph操作主要利用顶点和边进行判断,返回的新图中包含满足判断要求的顶点、边。该操作常用于一些情景,比如:限制感兴趣的图顶点和边,删除损坏连接。如下代码:

  1. /***********************************
  2. *展示结构操作
  3. ***********************************/
  4. graph.triplets.map(
  5. triplet => triplet.srcAttr._1 + " is the " + triplet.attr + " of " + triplet.dstAttr._1
  6. ).collect.foreach(println(_))
  7. println("---------------------------------------------")
  8. println("删除不存在的节点,构建子图")
  9. val validGraph = graph.subgraph(vpred = (id, attr) => attr._2 != "Missing")
  10. validGraph.vertices.collect.foreach(println(_))
  11. validGraph.triplets.map(
  12. triplet => triplet.srcAttr._1 + " is the " + triplet.attr + " of " + triplet.dstAttr._1
  13. ).collect.foreach(println(_))
  14. println("---------------------------------------------")
  15. println("构建职业是professor的子图,并打印子图的顶点")
  16. val subGraph = graph.subgraph(vpred = (id, attr) => attr._2 == "prof")
  17. subGraph.vertices.collect.foreach(v => println(s"${v._2._1} is ${v._2._2}"))
scala

mask 操作构造一个子图,这个子图包含输入图中包含的顶点和边。这个操作可以和subgraph操作相结合,基于另外一个相关图的特征去约束一个图。例如,我们可以使用丢失顶点的图运行连通分支,然后限制有效子图的返回,见如下代码:

  1. println("---------------------------------------------")
  2. println("运行联通分支")
  3. val ccGraph = graph.connectedComponents()
  4. val validCCGraph = ccGraph.mask(validGraph)
scala

groupEdges 操作合并多重图中的并行边(如顶点对之间重复的边)。在大量的 应用程序中,并行的边可以合并(它们的权重合并)为一条边从而降低图的大小。

关联操作

在很多情况下,需要将外部数据添加到图中。例如,我们可能有额外的用户属性,我们想把它融合到一个存在图中或者从一个图提取数据属性到另一个图。这些任务可以使用join操作来实现。下面我们列出了关键的join操作:

  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. }
scala

joinVertices操作连接外部RDD的顶点,返回一个新的带有顶点特征的图。这些特征是通过在连接顶点的结果上使用用户自定义的 map 函数获得的。没有 匹配的顶点保留其原始值。
outerJoinVertices操作和joinVertices操作相似,但用户自定义的map函数可以被应用到所有顶点和改变顶点类型。

  1. /***********************************
  2. *展示关联操作
  3. ***********************************/
  4. println("**********************************************************")
  5. println("关联操作")
  6. println("**********************************************************")
  7. val inDegrees: VertexRDD[Int] = graph.inDegrees
  8. case class User(name: String, occupation: String, inDeg: Int, outDeg: Int)
  9. //创建一个新图,顶点VD的数据类型为User,并从graph做类型转换
  10. val initialUserGraph: Graph[User, String] = graph.mapVertices { case (id, (name, occupation)) => User(name, occupation , 0, 0)}
  11. //initialUserGraph与inDegrees、outDegrees(RDD)进行连接,并修改initialUserGraph中inDeg值、outDeg值
  12. val userGraph = initialUserGraph.outerJoinVertices(initialUserGraph.inDegrees) {
  13. case (id, u, inDegOpt) => User(u.name, u.occupation, inDegOpt.getOrElse(0), u.outDeg)
  14. }.outerJoinVertices(initialUserGraph.outDegrees) {
  15. case (id, u, outDegOpt) => User(u.name, u.occupation, u.inDeg,outDegOpt.getOrElse(0))
  16. }
  17. println("连接图的属性:")
  18. userGraph.vertices.collect.foreach(v => println(s
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!