Gremlin query to override the Vertex

瘦欲@ 提交于 2019-12-08 08:03:50

问题


How to write gremlin query which will do

  1. Get existing vertex
  2. Remove all existing properties(obviously except id, label, index property etc)
  3. Add new properties with values(mostly string only)

Or is there any alternate way to override the vertex using gremlin query?


回答1:


Using the TinkerPop modern graph as an example:

gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],age:[29]]

and assuming full Gremlin support you could keep the "name" property (i.e. index property) remove all other properties and add new properties in a single line of Gremlin as follows:

gremlin> g.V().has('person','name','marko').
......1>   sideEffect(properties().not(hasKey('name')).drop()).
......2>   property('age',33).
......3>   property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]

But in CosmosDB, I don't think you yet have support for the sideEffect() step. The trick is that to do this in one traversal is that you need to "side-effect" the drop() in some way and since drop() behaves as a filter everything step, any steps you add after it will simply not execute as there is nothing left in the stream to operate on.

Some workaround ideas for the lack of sideEffect() include using union() with identity():

gremlin> g.V().has('person','name','marko').
......1>   union(properties().not(hasKey('name')).drop(),
......2>         __.identity()).
......3>   property('age',33).
......4>   property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]

Less readable than the intent of sideEffect() but effective. I believe CosmosDB supports identity() even though it is not documented on their web site. If you don't have identity() you just have to get creative I suppose - here's another way which is even less nice:

gremlin> g.V().has('person','name','marko').as('a').
......1>   union(properties().not(hasKey('name')).drop(),
......2>         select('a')).
......3>   property('age',33).
......4>   property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]


来源:https://stackoverflow.com/questions/54089178/gremlin-query-to-override-the-vertex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!