How to overwrite vertices ID in Titan Database?

2019-02-24 17:31发布

问题:

I'm using a framework that generates objects Node and they already have assigned a id. Now they need to be converted to Titan vertices with the same ID controlled in the framework (accessed with node.id)

public long addNode(Node node) {    
   TitanVertex vertex = (TitanVertex) g.addVertex(null);
   g.commit();

   vertex.setProperty(ID, node.id);
   vertex.setProperty(TYPE, node.type);
   vertex.setProperty(VERSION, node.version);
   vertex.setProperty(TIME, node.time);
   vertex.setProperty(DATA, node.data);
   ...

Error:

java.lang.IllegalArgumentException: Name is reserved: id

But it seems to not allow it. Should I use some fake property to imitate a secondary Id? Does Titan has some way to do that?

Thanks!

回答1:

Very few graph databases actually allow you to set the element identifier. They all tend to have their own ID systems whether you are using Neo4j, OrientDB, Titan, etc. TinkerGraph is really the only Blueprints implementation that allows ID assignment.

If you want to keep your ID, then you should simply rename it to something else. Instead of "id", perhaps you could use "iid". To make things more transparent, from a programming perspective, you might consider use of the IdGraph wrapper, which would allow you to do something like:

gremlin> base = TitanFactory.open('/tmp/titan-berkley')
==>titangraph[local:/tmp/titan-berkley]
gremlin> g = new IdGraph(base, true, false)            
==>idgraph[titangraph[local:/tmp/titan-berkley]]
gremlin> g.addVertex(45)  
==>v[45]
gremlin> g.v(45)
==>v[45]

You can see IdGraph allows it to appear as though you are assigning the element id itself. Behind the scenes it is actually just using key indices.



回答2:

@Stephen, Cant say about the gremlin terminal, but tried this through Titan Java API and it didnt work. Even after passing id's while creating vertices in the id graph, default id's were assigned to nodes.