【问题标题】:Graphx lookup vertexLabel from vertexIDGraphx 从 vertexID 查找 vertexLabel
【发布时间】:2026-02-02 15:35:01
【问题描述】:

我从 apache spark graphx 创建了一个图形,其顶点格式为
(vertexId,vertexLabel)

graph.vertices.take(5)
(73607571123990017,157.55.145.210)
(-8476294060085646488,65.55.116.184)
(-1290863642671546500,184.73.235.12)
(4333023396065188982,63.91.215.17)
(-8653425046038876102,23.62.195.78)

我已经计算了一个顶点的单源最短路径,其语法和输出如下所示

(dstID,(length,List(whole path))

sssp.vertices.take(5)
(-912545243459764830,(3,List(223277346867836574, -7175187973700249964, 3342971904799511809, -912545243459764830)))
(2186653685768931954,(1000,List()))
(-5644725372565726221,(1000,List()))
(4398516124184853312,(3,List(223277346867836574, -7175187973700249964, 3342971904799511809, 4398516124184853312)))
(-7175187973700249964,(1,List(223277346867836574, -7175187973700249964)))

我想从 vertexId
(例如 73607571123990017)中查找 vertexLabel(例如 157.55.145.210),这样sssp.vertices.take(5) 的输出如下所示

(145.22.33.456,(3,List(155.22.32.938, 185.42.53.756, 105.62.83.956, 125.26.73.656)))

我已经尝试过这样的事情,但它只适用于一个单独的 vertexId,而不适用于 sssp.vertices.take(5) 的整个输出

graph.vertices.filter{case(id, _) => id==223277346867836574L}.collect

我应该如何以上述方式输出最短路径?

【问题讨论】:

    标签: scala apache-spark hash lookup spark-graphx


    【解决方案1】:

    您必须与原始图表执行连接才能取回标签。 我会将所有路径平面映射到一个巨大的 RDD 中并进行一次连接以获取标签。路径为空的顶点应与另一个连接分开处理。

    val pathsRDD = sssp.vertices.values.flatMap { case (_, vertices) =>
      if (vertices.isEmpty) {
        Seq.Empty
      } else {
        val dest = vertices.last
        // Store an index so we can reconstruct the list in the correct order
        vertices.zipWithIndex.map { case (v, index) => 
          (v, (dest, index))
        }
      }
    }
    
    pathsRDD.join(graph.vertices).map { case (vertex, ((dest, index), label)) =>
      (dest, (label, index)) 
    }.groupByKey.map { case (dest, iter) =>
      val out = iter.toList.sortBy(_._2).map(_._1)
      (out.last, (out.size, out))
    }
    

    【讨论】: