如何在Gremlin/Tinkerpop中的不同顶点上执行交叉连接



我是图形数据库的新手。这可能是一个非常基本的问题,但任何帮助都非常感谢。我正在使用Gremlin/Tinkerpop 3来查询存储在TitanDB中的图形。

以下是我的数据集。

User            |  associated_ids
---------------------------------
    A           | [ 1, 2 ]
    B           | [ 3, 4 ]
    c           | [ 5 ]
    D           | [ 4, 2 ]

现在想要获取与任何associated_id相互链接的所有用户组。这里 [A] 和 [D] 是直接链接的,因为两者都有共同的实体 [2],而 [B] 和 [D] 是直接链接的,因为两者都有共同的实体 [4]。因此,间接地[A],[B]和[D]是联系在一起的。

预期结果类似。

    Users       |  associated_ids
    ----------------------------
    A,B,D       | [ 1, 2 ,3, 4]
    c           | [ 5 ]

以下是我为生成图形而执行的步骤。

    a = graph.addVertex(label, "person", "user", "A")
    b = graph.addVertex(label, "person", "user", "B")
    c = graph.addVertex(label, "person", "user", "C")
    d = graph.addVertex(label, "person", "user", "D")
    one = graph.addVertex('rec_id')
    one.property('ids', '1')
    two = graph.addVertex('rec_id')
    two.property('ids', '2')
    three = graph.addVertex('rec_id')
    three.property('ids', '3')
    four = graph.addVertex('rec_id')
    four.property('ids', '4')
    five = graph.addVertex('rec_id')
    five.property('ids', '5')
    a.addEdge('part_of',one)
    a.addEdge('part_of',two)
    b.addEdge('part_of', three)
    b.addEdge('part_of',four)
    c.addEdge('part_of',five)
    d.addEdge('part_of',four)
    d.addEdge('part_of',two)

现在想知道我如何通过任何rec_id获得彼此关联的所有用户。

Gremlin 查询如何获得所需的输出?

另外,如果我需要对图形结构进行任何更改以满足我的要求,请告诉我。

你基本上在寻找的是连接的组件。

gremlin> g.V().
           emit(cyclicPath().or().not(both())).
             repeat(both()).
             until(cyclicPath()).
           aggregate("p").by(path()).cap("p").
           unfold().limit(local, 1).dedup().
           map(__.as("v").select("p").unfold().
                  filter(unfold().where(eq("v"))).
                  unfold().dedup().order().by(id).fold()).dedup().
           project("Users","associated_ids").
             by(unfold().hasLabel("person").values("user").fold()).
             by(unfold().hasLabel("rec_id").values("ids").fold())
==>[Users:[A,B,D],associated_ids:[1,2,3,4]]
==>[Users:[C],associated_ids:[5]]

以下是获得所需答案的一种方法:

gremlin> g.V().has('rec_id','ids','1').
......1>   in().aggregate('s').
......2>   repeat(out().in().where(without('s')).aggregate('s')).
......3>   cap('s').unfold().valueMap()
==>[user:[A]]
==>[user:[D]]
==>[user:[B]]
gremlin> g.V().has('rec_id','ids','5').
......1>   in().aggregate('s').
......2>   repeat(out().in().where(without('s')).aggregate('s')).
......3>   cap('s').unfold().valueMap()
==>[user:[C]]

因此,在遍历图形时,您基本上将用户顶点组保持在"s"中。标记为"1>"的行显示您获取您请求的"rec_id"中的初始用户的位置。在第"2>"行中,您递归地遍历这些用户并找到他们的"rec_ids",并忽略您已经遇到的存储在"s"中的任何用户。"3>"的最后一行从遍历中提取了"s"的副作用,为了方便起见,扩展了顶点列表并对其运行valueMap(),以便我们很好地看到"user"属性。

最新更新