• spark graphx


    在介绍完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.  
    10. //创建一个新图,顶点VD的数据类型为User,并从graph做类型转换
    11. val initialUserGraph: Graph[User, String] = graph.mapVertices { case (id, (name, occupation)) => User(name, occupation , 0, 0)}
    12.  
    13. //initialUserGraph与inDegrees、outDegrees(RDD)进行连接,并修改initialUserGraph中inDeg值、outDeg值
    14. val userGraph = initialUserGraph.outerJoinVertices(initialUserGraph.inDegrees) {
    15. case (id, u, inDegOpt) => User(u.name, u.occupation, inDegOpt.getOrElse(0), u.outDeg)
    16. }.outerJoinVertices(initialUserGraph.outDegrees) {
    17. case (id, u, outDegOpt) => User(u.name, u.occupation, u.inDeg,outDegOpt.getOrElse(0))
    18. }
    19.  
    20. println("连接图的属性:")
    21. userGraph.vertices.collect.foreach(v => println(s"${v._2.name} inDeg: ${v._2.inDeg} outDeg: ${v._2.outDeg}"))
    22.  
    23. println("出度和入度相同的人员:")
    24. userGraph.vertices.filter {
    25. case (id, u) => u.inDeg == u.outDeg
    26. }.collect.foreach {
    27. case (id, property) => println(property.name)
    28. }
    scala

    聚合操作

    在很多图分析任务中一个关键步骤就是集合每一个顶点的邻居信息。例如,我们想知道每一个用户的追随者数量或者追随者的平均年龄。一些迭代的图算法(像PageRank,最短路径和联通组件)就需要反复得聚合相邻顶点的属性。

    信息聚合(aggregateMessages)

    在GraphX中最核心的聚合操作就是aggregateMessages.它主要功能是向邻边发消息,合并邻边收到的消息.

    1. class Graph[VD, ED] {
    2. def aggregateMessages[Msg: ClassTag](
    3. sendMsg: EdgeContext[VD, ED, Msg] => Unit,
    4. mergeMsg: (Msg, Msg) => Msg,
    5. tripletFields: TripletFields = TripletFields.All)
    6. : VertexRDD[Msg]
    7. }
    scala

    接口中含有三个参数,分别表示:
    sendMsg:发消息函数
    mergeMsg:合并消息函数
    tripletFields:发消息的方向
    下面使用aggregateMessages来计算比用户年龄更大的追随者的平均年龄,这里我们新建一个模型图代替上一节创建的模型的图,整体代码如下:

    1. import org.apache.log4j.{Level,Logger}
    2. import org.apache.spark._
    3. import org.apache.spark.graphx._
    4. import org.apache.spark.graphx.util.GraphGenerators
    5. object SimpleGraphX {
    6. def main(args: Array[String]) {
    7. //屏蔽日志
    8. Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    9. Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
    10. //设置运行环境
    11. val conf = new SparkConf().setAppName("SimpleGraphX").setMaster("local")
    12. val sc = new SparkContext(conf)
    13. val graph: Graph[Double, Int] =
    14. GraphGenerators.logNormalGraph(sc, numVertices = 100).mapVertices( (id, _) => id.toDouble )
    15. // Compute the number of older followers and their total age
    16. val olderFollowers: VertexRDD[(Int, Double)] = graph.aggregateMessages[(Int, Double)](
    17. triplet => { // Map Function
    18. if (triplet.srcAttr > triplet.dstAttr) {
    19. // Send message to destination vertex containing counter and age
    20. triplet.sendToDst(1, triplet.srcAttr)
    21. }
    22. },
    23. // Add counter and age
    24. (a, b) => (a._1 + b._1, a._2 + b._2) // Reduce Function
    25. )
    26. val avgAgeOfOlderFollowers: VertexRDD[Double] =
    27. olderFollowers.mapValues( (id, value) =>
    28. value match { case (count, totalAge) => totalAge / count } )
    29. avgAgeOfOlderFollowers.collect.foreach(println(_))
    30. }
    31. }
    scala

    计算每个顶点的度

    在上一节中,就已经用了计算每个顶点的度(每一个顶点边的数量)的实例,这也是一种常见的聚合操作。在有向图的情况下,它经常知道入度,出度和每个顶点的总度。 GraphOps 类包含了每一个顶点的一系列的度的计算。例如:在下面将计算最大入度,出度和总度:

    1. def max(a: (VertexId, Int), b: (VertexId, Int)): (VertexId, Int) = {
    2. if (a._2 > b._2) a else b
    3. }
    4. val maxInDegree: (VertexId, Int) = graph.inDegrees.reduce(max)
    5. val maxOutDegree: (VertexId, Int) = graph.outDegrees.reduce(max)
    6. val maxDegrees: (VertexId, Int) = graph.degrees.reduce(max)
    scala

    缓存操作

    Spark中,RDDs默认是没有持久存储在内存。当多次使用RDDs时,为了避免重复计算,RDDs必须被显式缓存。GraphX中的图也是相同的方式,当使用一个图多次时,首先确认调用Graph.cache()。
    对于迭代计算来说,迭代的中间结果将填充到缓存中。虽然最终会被删除,但是保存在内存中的不需要的数据将会减慢垃圾回收。对于迭代计算,我们建议使用 Pregel API,它可以正确的不持久化中间结果。

    Spark GraphX常见的操作到此结束,接下来请访问Spark2.1.0入门:Spark GraphX算法实例


    在介绍完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.  
    10. //创建一个新图,顶点VD的数据类型为User,并从graph做类型转换
    11. val initialUserGraph: Graph[User, String] = graph.mapVertices { case (id, (name, occupation)) => User(name, occupation , 0, 0)}
    12.  
    13. //initialUserGraph与inDegrees、outDegrees(RDD)进行连接,并修改initialUserGraph中inDeg值、outDeg值
    14. val userGraph = initialUserGraph.outerJoinVertices(initialUserGraph.inDegrees) {
    15. case (id, u, inDegOpt) => User(u.name, u.occupation, inDegOpt.getOrElse(0), u.outDeg)
    16. }.outerJoinVertices(initialUserGraph.outDegrees) {
    17. case (id, u, outDegOpt) => User(u.name, u.occupation, u.inDeg,outDegOpt.getOrElse(0))
    18. }
    19.  
    20. println("连接图的属性:")
    21. userGraph.vertices.collect.foreach(v => println(s"${v._2.name} inDeg: ${v._2.inDeg} outDeg: ${v._2.outDeg}"))
    22.  
    23. println("出度和入度相同的人员:")
    24. userGraph.vertices.filter {
    25. case (id, u) => u.inDeg == u.outDeg
    26. }.collect.foreach {
    27. case (id, property) => println(property.name)
    28. }
    scala

    聚合操作

    在很多图分析任务中一个关键步骤就是集合每一个顶点的邻居信息。例如,我们想知道每一个用户的追随者数量或者追随者的平均年龄。一些迭代的图算法(像PageRank,最短路径和联通组件)就需要反复得聚合相邻顶点的属性。

    信息聚合(aggregateMessages)

    在GraphX中最核心的聚合操作就是aggregateMessages.它主要功能是向邻边发消息,合并邻边收到的消息.

    1. class Graph[VD, ED] {
    2. def aggregateMessages[Msg: ClassTag](
    3. sendMsg: EdgeContext[VD, ED, Msg] => Unit,
    4. mergeMsg: (Msg, Msg) => Msg,
    5. tripletFields: TripletFields = TripletFields.All)
    6. : VertexRDD[Msg]
    7. }
    scala

    接口中含有三个参数,分别表示:
    sendMsg:发消息函数
    mergeMsg:合并消息函数
    tripletFields:发消息的方向
    下面使用aggregateMessages来计算比用户年龄更大的追随者的平均年龄,这里我们新建一个模型图代替上一节创建的模型的图,整体代码如下:

    1. import org.apache.log4j.{Level,Logger}
    2. import org.apache.spark._
    3. import org.apache.spark.graphx._
    4. import org.apache.spark.graphx.util.GraphGenerators
    5. object SimpleGraphX {
    6. def main(args: Array[String]) {
    7. //屏蔽日志
    8. Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    9. Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
    10. //设置运行环境
    11. val conf = new SparkConf().setAppName("SimpleGraphX").setMaster("local")
    12. val sc = new SparkContext(conf)
    13. val graph: Graph[Double, Int] =
    14. GraphGenerators.logNormalGraph(sc, numVertices = 100).mapVertices( (id, _) => id.toDouble )
    15. // Compute the number of older followers and their total age
    16. val olderFollowers: VertexRDD[(Int, Double)] = graph.aggregateMessages[(Int, Double)](
    17. triplet => { // Map Function
    18. if (triplet.srcAttr > triplet.dstAttr) {
    19. // Send message to destination vertex containing counter and age
    20. triplet.sendToDst(1, triplet.srcAttr)
    21. }
    22. },
    23. // Add counter and age
    24. (a, b) => (a._1 + b._1, a._2 + b._2) // Reduce Function
    25. )
    26. val avgAgeOfOlderFollowers: VertexRDD[Double] =
    27. olderFollowers.mapValues( (id, value) =>
    28. value match { case (count, totalAge) => totalAge / count } )
    29. avgAgeOfOlderFollowers.collect.foreach(println(_))
    30. }
    31. }
    scala

    计算每个顶点的度

    在上一节中,就已经用了计算每个顶点的度(每一个顶点边的数量)的实例,这也是一种常见的聚合操作。在有向图的情况下,它经常知道入度,出度和每个顶点的总度。 GraphOps 类包含了每一个顶点的一系列的度的计算。例如:在下面将计算最大入度,出度和总度:

    1. def max(a: (VertexId, Int), b: (VertexId, Int)): (VertexId, Int) = {
    2. if (a._2 > b._2) a else b
    3. }
    4. val maxInDegree: (VertexId, Int) = graph.inDegrees.reduce(max)
    5. val maxOutDegree: (VertexId, Int) = graph.outDegrees.reduce(max)
    6. val maxDegrees: (VertexId, Int) = graph.degrees.reduce(max)
    scala

    缓存操作

    Spark中,RDDs默认是没有持久存储在内存。当多次使用RDDs时,为了避免重复计算,RDDs必须被显式缓存。GraphX中的图也是相同的方式,当使用一个图多次时,首先确认调用Graph.cache()。
    对于迭代计算来说,迭代的中间结果将填充到缓存中。虽然最终会被删除,但是保存在内存中的不需要的数据将会减慢垃圾回收。对于迭代计算,我们建议使用 Pregel API,它可以正确的不持久化中间结果。

    Spark GraphX常见的操作到此结束,接下来请访问Spark2.1.0入门:Spark GraphX算法实例

    PageRank算法

    PageRank,有成网页排名算法。PageRank通过网络的超链接关系确定网页的等级好坏,在搜索引擎优化操作中常用来评估网页的相关性和重要性。
    PageRank同样可以在图中测量每个顶点的重要性,假设存在一条从顶点u到顶点v的边,就代表顶点u对顶点v的支持。例如:微博中,一个用户被其他很多用户关注,那么这个用户的排名将会很高。
    GraphX 自带静态和动态的PageRank算法实现。静态的PageRank算法运行在固定的迭代次数,动态的PageRank算法运行直到整个排名收敛(eg:通过限定可容忍的值来停止迭代).
    利用GraphX自带的社会网络数据集实例,用户集合数据集存在/usr/local/Spark/data/graphx/users.txt,用户关系数据集存在/usr/local/Spark/data/graphx/followers.txt。现在计算每个用户的PageRank:

    1. import org.apache.log4j.{Level,Logger}
    2. import org.apache.spark._
    3. import org.apache.spark.graphx.GraphLoader
    4. object SimpleGraphX {
    5. def main(args: Array[String]) {
    6. //屏蔽日志
    7. Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    8. Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
    9. //设置运行环境
    10. val conf = new SparkConf().setAppName("SimpleGraphX").setMaster("local")
    11. val sc = new SparkContext(conf)
    12. // Load the edges as a graph
    13. val graph = GraphLoader.edgeListFile(sc, "file:///usr/local/Spark/data/graphx/followers.txt")
    14. // Run PageRank
    15. val ranks = graph.pageRank(0.0001).vertices
    16. // Join the ranks with the usernames
    17. val users = sc.textFile("file:///usr/local/Spark/data/graphx/users.txt").map { line =>
    18. val fields = line.split(",")
    19. (fields(0).toLong, fields(1))
    20. }
    21. val ranksByUsername = users.join(ranks).map {
    22. case (id, (username, rank)) => (username, rank)
    23. }
    24. // Print the result
    25. println(ranksByUsername.collect().mkString(" "))
    26. }
    27. }
    scala

    连通分支算法

    连通分支算法使用最小编号的顶点来标记每个连通分支。
    在一个社会网络,连通图近似簇。这里我们计算一个连通分支实例,所使用的数据集和PageRank一样。

    1. import org.apache.log4j.{Level,Logger}
    2. import org.apache.spark._
    3. import org.apache.spark.graphx.GraphLoader
    4. object SimpleGraphX {
    5. def main(args: Array[String]) {
    6. //屏蔽日志
    7. Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    8. Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
    9. //设置运行环境
    10. val conf = new SparkConf().setAppName("SimpleGraphX").setMaster("local")
    11. val sc = new SparkContext(conf)
    12. // Load the edges as a graph
    13. // Load the graph as in the PageRank example
    14. val graph = GraphLoader.edgeListFile(sc, "file:///usr/local/Spark/data/graphx/followers.txt")
    15. // Find the connected components
    16. val cc = graph.connectedComponents().vertices
    17. // Join the connected components with the usernames
    18. val users = sc.textFile("file:///usr/local/Spark/data/graphx/users.txt").map { line =>
    19. val fields = line.split(",")
    20. (fields(0).toLong, fields(1))
    21. }
    22. val ccByUsername = users.join(cc).map {
    23. case (id, (username, cc)) => (username, cc)
    24. }
    25. // Print the result
    26. println(ccByUsername.collect().mkString(" "))
    27. }
    28. }
    scala

    三角形计算算法

    在图中,如果一个顶点有两个邻接顶点并且顶点与顶点之间有边相连,那么我们就可以把三个顶点归于一个三角形。
    这里通过计算社交网络图中三角形的数量,所采用的数据集同样和PageRank的数据集一样。

    1. import org.apache.log4j.{Level,Logger}
    2. import org.apache.spark._
    3. import org.apache.spark.graphx.{GraphLoader,PartitionStrategy}
    4. object SimpleGraphX {
    5. def main(args: Array[String]) {
    6. //屏蔽日志
    7. Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    8. Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
    9. //设置运行环境
    10. val conf = new SparkConf().setAppName("SimpleGraphX").setMaster("local")
    11. val sc = new SparkContext(conf)
    12. // Load the edges in canonical order and partition the graph for triangle count
    13. val graph = GraphLoader.edgeListFile(sc, "file:///usr/local/Spark/data/graphx/followers.txt", true)
    14. .partitionBy(PartitionStrategy.RandomVertexCut)
    15. // Find the triangle count for each vertex
    16. val triCounts = graph.triangleCount().vertices
    17. // Join the triangle counts with the usernames
    18. val users = sc.textFile("file:///usr/local/Spark/data/graphx/users.txt").map { line =>
    19. val fields = line.split(",")
    20. (fields(0).toLong, fields(1))
    21. }
    22. val triCountByUsername = users.join(triCounts).map { case (id, (username, tc)) =>
    23. (username, tc)
    24. }
    25. // Print the result
    26. println(triCountByUsername.collect().mkString(" "))
    27.  
    28. }
    29. }

    转自:http://dblab.xmu.edu.cn/blog/spark/


    正因为当初对未来做了太多的憧憬,所以对现在的自己尤其失望。生命中曾经有过的所有灿烂,终究都需要用寂寞来偿还。
  • 相关阅读:
    MySQL学习之——锁(行锁、表锁、页锁、乐观锁、悲观锁等)
    MySQL 行锁 表锁机制
    数据库中悲观锁和乐观锁
    NYOJ 116 士兵杀敌(二)【线段树 单点更新】
    java模拟而一个电话本操作
    Protostuff具体解释
    Java之enum
    muduo::Connector、TcpClient分析
    Android性能优化之中的一个 布局优化
    linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
  • 原文地址:https://www.cnblogs.com/candlia/p/11920132.html
Copyright © 2020-2023  润新知