UPSERTing with TitanDB

后端 未结 2 1815
小蘑菇
小蘑菇 2021-01-23 17:09

I\'m having my first steps as a TitanDB user. That being, I\'d like to know how to make an upsert / conditionally insert a vertex inside a TitanTransaction (in the

2条回答
  •  灰色年华
    2021-01-23 17:16

    Here's a one-liner "getOrCreate" for Titan 1.0 and TinkerPop 3:

    getOrCreate = { id ->
      g.V().has('userId', id).tryNext().orElseGet{ g.addV('userId', id).next() }
    }
    

    As taken from the new TinkerPop "Getting Started" Tutorial. Here is the same code translated to java:

    public Vertex getOrCreate(Object id) {
      return g.V().has('userId', id).tryNext().orElseGet(() -> g.addV('userId', id).next());
    }
    

提交回复
热议问题