GraphX-从路径检索所有节点 [英] GraphX - Retrieving all nodes from a path

查看:258
本文介绍了GraphX-从路径检索所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GraphX中,是否可以检索路径上具有一定长度的所有节点和边?

In GraphX, is there a way to retrieve all the nodes and edges that are on a path that are of a certain length?

更具体地说,我想获得从A到B的所有10步路径. 对于每个路径,我想获取节点和边的列表.

More specifically, I would like to get all the 10-step paths from A to B. For each path, I would like to get the list of nodes and edges.

谢谢.

推荐答案

免责声明:仅用于显示GraphFrames路径过滤功能.

Disclaimer: This is only intended to show GraphFrames path filtering capabilities.

从理论上讲,这是可能的.您可以使用 GraphFrames 模式查找路径.假设您的数据如下所示:

Well, theoretically speaking it is possible. You can use GraphFrames patterns to find paths. Lets assume your data looks as follows:

import org.graphframes.GraphFrame

val nodes = "abcdefghij".map(c =>Tuple1(c.toString)).toDF("id")

val edges = Seq(
   // Long path
  ("a", "b"), ("b", "c"), ("c", "d"),  ("d", "e"), ("e", "f"),
  // and some random nodes
  ("g", "h"), ("i", "j"), ("j", "i")
).toDF("src", "dst")

val gf = GraphFrame(nodes, edges)

,并且您要查找具有至少5个节点的所有路径.

and you want to find all paths with at least 5 nodes.

您可以构建以下路径模式:

You can construct following path pattern:

val path = (1 to 4).map(i => s"(n$i)-[e$i]->(n${i + 1})").mkString(";")
// (n1)-[e1]->(n2);(n2)-[e2]->(n3);(n3)-[e3]->(n4);(n4)-[e4]->(n5)

并过滤表达式以避免循环:

and filter expression to avoid cycles:

val expr = (1 to 5).map(i => s"n$i").combinations(2).map {
  case Seq(i, j) => col(i) !== col(j)
}.reduce(_ && _)

最后快速检查:

gf.find(path).where(expr).show
// +-----+---+---+-----+---+-----+---+-----+---+
// |   e1| n1| n2|   e2| n3|   e3| n4|   e4| n5|
// +-----+---+---+-----+---+-----+---+-----+---+
// |[a,b]|[a]|[b]|[b,c]|[c]|[c,d]|[d]|[d,e]|[e]|
// |[b,c]|[b]|[c]|[c,d]|[d]|[d,e]|[e]|[e,f]|[f]|
// +-----+---+---+-----+---+-----+---+-----+---+

这篇关于GraphX-从路径检索所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆