Scala 在数据库中插入数据时获取重复项

Scala 在数据库中插入数据时获取重复项,scala,titan,Scala,Titan,下面是我用来将数据插入titan的Scala代码 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.f

下面是我用来将数据插入titan的Scala代码

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))+"');") 
   }
 }

for(n我不确定在Scala中如何做到这一点,但根据某些属性,确保顶点唯一的一个好方法是将该属性索引为唯一

如上所述,您可以在顶点
myId
上创建一个属性,并告诉架构它是唯一的。这样,您可以通过该id进行快速查找,更重要的是,让Titan确保顶点通过该属性是唯一的。例如:

//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();
上面的代码将告诉Titan,您有一个名为
myId
的属性,该属性应该是唯一的。如果您执行以下操作,则该属性应该是唯一的:

graph.addVertex().property("myId", 1); 
graph.addVertex().property("myId", 1);
Titan将失败并警告您重复