如何使用Apache Gremlin 3.5.2在AWS Neptune中合并两个顶点?
使用Gremlin合并AWS Neptune中的顶点
要将两个TopVertex(id均为4713)合并为一个包含全部6个SubVertex的mergedVertex,可以按以下三步操作:
1. 创建合并后的顶点并整合属性
首先创建新的TopVertex,将两个原顶点的属性(testProperty1和testProperty2)都整合进去:
g.V().hasLabel('TopVertex').has('id', 4713).fold() .coalesce( // 检查是否已存在包含双属性的顶点,避免重复创建 unfold().has('testProperty1').has('testProperty2'), // 创建新顶点,复制两个原顶点的属性 addV('TopVertex').property('id', 4713) .property('testProperty1', g.V().hasLabel('TopVertex').has('id',4713).has('testProperty1').values('testProperty1').next()) .property('testProperty2', g.V().hasLabel('TopVertex').has('id',4713).has('testProperty2').values('testProperty2').next()) ).as('merged')
2. 迁移原顶点的关联边到新顶点
将两个原顶点指向SubVertex的splitsInto边,连同边的ordinal属性一起迁移到新顶点:
// 迁移第一个原顶点的边 g.V().hasLabel('TopVertex').has('id',4713).has('testProperty1','testProperty1') .outE('splitsInto') .addE('splitsInto').from(g.V().hasLabel('TopVertex').has('id',4713).has('testProperty1').has('testProperty2')) .to(__.inV()) .property('ordinal', __.values('ordinal')) .drop() // 迁移第二个原顶点的边 g.V().hasLabel('TopVertex').has('id',4713).has('testProperty2','testProperty2') .outE('splitsInto') .addE('splitsInto').from(g.V().hasLabel('TopVertex').has('id',4713).has('testProperty1').has('testProperty2')) .to(__.inV()) .property('ordinal', __.values('ordinal')) .drop()
3. 删除原顶点
确认边迁移完成后,删除原来的两个TopVertex:
// 删除第一个原顶点 g.V().hasLabel('TopVertex').has('id',4713).has('testProperty1','testProperty1').drop() // 删除第二个原顶点 g.V().hasLabel('TopVertex').has('id',4713).has('testProperty2','testProperty2').drop()
注意事项
- 执行前可以先通过
g.V().hasLabel('TopVertex').has('id',4713).valueMap()查询原顶点属性,确保属性值正确。 - 所有操作建议在事务中执行,避免中途出错导致数据不一致。
内容的提问来源于stack exchange,提问作者r3dup
相关产品推荐
相关产品推荐

