Balaji Reddy
Balaji Reddy

Reputation: 5700

how to avoid duplicate vertex entries in DSE graph/Titan

I have the following graph definition.

schema.propertyKey("ID").text().create()
schema.vertexLabel("Student").properties("ID").create()

When I execute the below Gremlin query, a new vertex is created.

g.addV(label, 'Student').property('ID', '1234')

when I execute it again, new Vertex with same ID has been created.I'm looking for a way to make ID value unique. Meaning I should get error when I try to add a new student with same ID(1234). Any help highly appreciated on this.

Upvotes: 2

Views: 890

Answers (2)

Florian Hockmann
Florian Hockmann

Reputation: 2809

I don't know about DSE Graph, but in Titan you can create an index and configure it to be unique. But it is not recommended to do that (at least not if it affects many vertices) as Titan has to use locks to insert vertices with such an index in order to avoid duplicates.

You will get a better performance if you check whether the vertex exists already before inserting it. Daniel Kuppitz provided a query for that on the mailing list [1]:

g.V().has('Student','ID','1234').tryNext().orElseGet(g.addV(T.label,'Student','ID', '1234').next())

Of course, you could get into race conditions here where two of those queries are evaluated for the same ID at the same time. But this should only occur very rarely and you can probably perform a regular clean-up with an OLAP job with an upcoming version of TinkerPop. (Unfortunately, it is currently not possible to modify the graph with an OLAP job.)

[1] https://groups.google.com/forum/#!topic/gremlin-users/pCYf6h3Frb8

Upvotes: 2

Alaa Mahmoud
Alaa Mahmoud

Reputation: 743

When you define your schema for your graph set the cardinality of the ID property to SINGLE

From the Titan schema docs

SINGLE: Allows at most one value per element for such key. In other words, the key→value mapping is unique for all elements in the graph. The property key birthDate is an example with SINGLE cardinality since each person has exactly one birth date.

Here's a link to the docs http://s3.thinkaurelius.com/docs/titan/1.0.0/schema.html

Upvotes: 1

Related Questions