首页 > 解决方案 > 如何在 GraphX 中使用 2 步连接计算入度

问题描述

我有一个有向图,比如 fromNode -> toNode。我想计算“toNode”上的总传入连接,包括 1 跳或 2 跳连接。

例如:如果有一个节点 Z 使得 X 连接 Z 并且 Z 连接 Y,则存在从节点 X 到节点 Y 的两跳连接。

标签: scalaspark-graphx

解决方案


我认为您可以使用 aggregateMessages() 两次。
第一步,收集每个节点的 inDegrees,并存储它们。
第二步,收集节点中的inDegrees信息到最终节点。
或者,您可以先使用 outerJoinVertices(),例如:

yourGraph = followerGraph.outerJoinVertices(
        yourGraph.ops().inDegrees(),
        new setDegreesMsg()//use the function to combine inDegrees info with yourGraph
);

然后,使用 aggregateMessages() 收集邻居的 inDegrees 信息,例如:

        degrees:RDD[(VertexId, Int)] = yourGraph.aggregateMessages(
                new getInfoSendMsg(),
                new getInfoMergeMsg()
);

推荐阅读