如何使用 Apache Gremlin 查询语言(版本 3.5.2)将两个顶点合并为一个?

问题描述 投票:0回答:1

以这个图模型为例:

g.addV("TopVertex").property('id', 4713).property('testProperty1','testProperty1').as('vertex1')
.addV("SubVertex").property('name','C1').as('C1')
.addV("SubVertex").property('name','C2').as('C2')
.addV("SubVertex").property('name','C3').as('C3')
.addE("splitsInto").from('vertex1').to('C1').property('ordinal',1)
.addE("splitsInto").from('vertex1').to('C2').property('ordinal',2)
.addE("splitsInto").from('vertex1').to('C3').property('ordinal',3)

.addV("TopVertex").property('id', 4713).property('testProperty2','testProperty2').as('vertex2')
.addV("SubVertex").property('name','C4').as('C4')
.addV("SubVertex").property('name','C5').as('C5')
.addV("SubVertex").property('name','C6').as('C6')
.addE("splitsInto").from('vertex2').to('C4').property('ordinal',4)
.addE("splitsInto").from('vertex2').to('C5').property('ordinal',5)
.addE("splitsInto").from('vertex2').to('C6').property('ordinal',6)

现在我想将 'vertex1' 和 'vertex2' 合并到一个 'mergedVertex',这样 'mergedVertex' 就有所有六个 SubVertex。我如何使用 Gremlin 查询语言和 AWS Neptune 数据库实现这一目标?

因为我对 Gremlin 不太熟悉,所以我基本上是反复试验解决方案

amazon-web-services graph gremlin amazon-neptune
1个回答
0
投票

现在这是我目前正在使用的解决方案,有什么改进建议吗?

编辑:进一步解释我的解决方案。我的目标是创建一个新的合并顶点并将旧两个顶点的边复制到新的合并顶点。

g.addV("MergedVertex").property('id', 4713).as('mergedVertex'). //add new vertex to traversal
V().hasLabel("TopVertex").has('id', 4713).as('oldVertices'). //get all vertices with the label "TopVertex" and the provided id
       outE().as('oldEdges'). //get all outgoing edges
       inV().as('inVertices'). // get all in vertices of these
       select('mergedVertex'). 
       addE('splitsInto').to('inVertices').as('newEdges'). //add new edges to the mergedVertex
       sideEffect(select('oldEdges').properties(). //the sideEffect section copies the properties of the old edges and pastes them into the newEdges
                  unfold().as('props').
                  select('newEdges').
                  property(select('props').key(), select('props').value())).
       select('oldVertices').drop() //with the last step we drop the old vertices
       
© www.soinside.com 2019 - 2024. All rights reserved.