UPSERTing with TitanDB

你。 提交于 2019-12-02 07:19:40

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());
}

Roughly speaking, every Cassandra insert is an "upsert". If you look at the Titan representation of vertices and edges in a Cassandra-like model, you'll find vertices and edges each get their own rows. This means a blind write of an edge will have the given behavior you're looking for: what you write is what will win. Doing this with a vertex isn't supported directly by Titan.

But I don't think this is what you're looking for. If you're looking to enforce uniqueness, why not use the unique() modifier on a Titan composite index? (From the documentation):

mgmt.buildIndex('byNameUnique', Vertex.class).addKey(name).unique().buildCompositeIndex()

With a Cassandra storage backend, you need to enable consistency locking. As with any database, managing the overhead of uniqueness comes at a cost which you need to consider when writing your data. This way if you insert a vertex which violates your uniqueness requirement, the transaction will fail.

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