zoukankan      html  css  js  c++  java
  • GraphX实现N度关系

    背景

    本文给出了一个简单的计算图中每一个点的N度关系点集合的算法,也就是N跳关系。

    之前通过官方文档学习和理解了一下GraphX的计算接口。

    N度关系

    目标:
    在N轮里。找到某一个点的N度关系的点集合。

    实现思路:
    1. 准备好边数据集。即”1 3”, “4, 1” 这种点关系。

    使用GraphLoader 的接口load成Graph
    2. 初始化每一个Vertice的属性为空Map
    3. 使用aggregateMessagesVerticeIDtotalRounds传播出度点上,出度点把收集到的信息合成一个大Map
    4. 更新后的Vertice与原图进行”Join”,更新图中的变化过的点属性
    5. 反复步骤3和4,最后输出更新了N轮之后的有关系的Vertice

    spark-shell下可运行的代码:

    import org.apache.spark._
    import org.apache.spark.graphx._
    import org.apache.spark.rdd.RDD
    
    val friendsGraph = GraphLoader.edgeListFile(sc, "data/friends.txt")
    val totalRounds: Int = 3 // total N round
    var targetVerticeID: Long = 6 // target vertice
    
    // round one
    var roundGraph = friendsGraph.mapVertices((id, vd) => Map())
    var roundVertices = roundGraph.aggregateMessages[Map[Long, Integer]](
      ctx => {
        if (targetVerticeID == ctx.srcId) {
          // only the edge has target vertice should send msg
          ctx.sendToDst(Map(ctx.srcId -> totalRounds))
        }
      }, 
      _ ++ _
    )
    
    for (i <- 2 to totalRounds) {
      val thisRoundGraph = roundGraph.outerJoinVertices(roundVertices){ (vid, data, opt) => opt.getOrElse(Map[Long, Integer]()) }
      roundVertices = thisRoundGraph.aggregateMessages[Map[Long, Integer]](
        ctx => {
          val iterator = ctx.srcAttr.iterator
          while (iterator.hasNext) {
            val (k, v) = iterator.next
            if (v > 1) {
              val newV = v - 1
              ctx.sendToDst(Map(k -> newV))
              ctx.srcAttr.updated(k, newV)
            } else {
              // do output and remove this entry
            }
          }
        },
        (newAttr, oldAttr) => {
          if (oldAttr.contains(newAttr.head._1)) { // optimization to reduce msg
            oldAttr.updated(newAttr.head._1, 1) // stop sending this ever
          } else {
            oldAttr ++ newAttr
          }
        }
      )
    }
    
    val result = roundVertices.map(_._1).collect

    数据和输出

    2 1
    4 1
    1 2
    6 3
    7 3
    7 6
    6 7
    3 7
    4 3
    1 6
    6 1
    Array(6, 1, 3, 7)

    总结

    实现的比較naive。还有很多能够优化的地方。

    全文完 :)

  • 相关阅读:
    XMPP即时通讯资料记录
    iOS 图片裁剪与修改
    iOS开发xcode报错:"xxxxxx"has been modified since the precompiled header was built
    模糊数学课件(清晰易懂)
    几个可用于数据挖掘和统计分析的java库
    java中list集合的内容,如何使用像数据库中group by形式那样排序
    spark java 代码example
    spark 编程向导
    一个深度学习博客
    Selenium2(WebDriver)_如何判断WebElement元素对象是否存在
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7353215.html
Copyright © 2011-2022 走看看