user3843520
user3843520

Reputation: 43

Getting duplicates entries while inserting data in titandb

Below is the Scala code that I am using to insert the data into titan.

for(n <- nodes){
   stringNodes = stringNodes.concat("v"+n.letter+" = graph.addVertex('"+n.label+"');") 
   for(a <- n.attributes){
     stringNodes = stringNodes.concat("v"+n.letter+".property('"+a.name+"','"+row(row.fieldIndex(a.name))+"');") 
   }
 }

The challenge we are facing is every time we are getting multiple entries of the one node. https://i.sstatic.net/cQ7wN.png

Can you please help and advise the best way to insert unique records in titan db?

Upvotes: 1

Views: 57

Answers (1)

Filipe Teixeira
Filipe Teixeira

Reputation: 3565

I am not sure of how you would do this in Scala but a good way of ensuring vertices are unique according to some property is to index that property to be unique.

As outlined here you could create a property on your vertex myId and tell the schema that it is unique. That way you can do quick lookups by that id and more importantly let Titan ensure that the vertex is unique by that property. For example:

//Open up managment interface
graph = TitanFactory.open(config);
mgmt = graph.openManagement();

//Create the property key
propKey = management.makePropertyKey("myId").dataType(String.class)).make();

//Tell Titan it should be unique
mgmt.buildIndex("byMyIdUnique", Vertex.class).addKey(propKey).unique().buildCompositeIndex();
mgmt.commit();

The above code will tell Titan that you have a property called myId which should be unique. That way if you ever do the following:

graph.addVertex().property("myId", 1); 
graph.addVertex().property("myId", 1);

Titan will fail and warn you of the duplicates.

Upvotes: 2

Related Questions