3

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!

Luccas
  • 4,078
  • 6
  • 42
  • 72

2 Answers2

8

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.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Nice answer! Thanks! The only problem for my context is that I would like to perform tests with and without key indexes for a benchmark. It indexes the vertex lookup in the database or just indexes the assignment between custom id x real id? Thanks again – Luccas Jul 01 '13 at 16:17
  • 1
    If you need to work without key indices, then create a property name using a name other than "id" (e.g. "iid"). You don't mention what your benchmark is, but without key indices, you will likely see some fairly poor results as lookups by "iid" will end up being linear scans. The question you ask I assume is related to how titan stores key indices themselves. If so, this link might help: https://github.com/elffersj/delftswa-aurelius-titan/blob/master/SA-doc/Mapping.md – stephen mallette Jul 01 '13 at 16:54
  • If you want custom ids on edges also IdGraph(base, true, true) then you may need to create the __id property before, I needed it in 0.3.0 https://github.com/thinkaurelius/titan/issues/179 – Gonfi den Tschal Jan 12 '14 at 21:09
  • I was also looking for this solution.Will there be any performance issue with `IdGraph` as we manually assign Id to vertex? – Manish Kumar Feb 06 '14 at 16:37
  • You wouldn't see any more of a performance penalty than you would for a normal index lookup on the graph (which you would have to do anyway to find something in the graph as you likely won't have the graph-assigned id handy in most cases). – stephen mallette Feb 06 '14 at 16:41
  • oh I found another solution `g.getVertices("vid", "id123");` I think this will be more natural to titan. Here `vid` will be the id of vertex. – Manish Kumar Feb 06 '14 at 16:51
  • 2
    `IdGraph` basically just does that under the covers, using a key index to look up the vertex. So when you do `g.getVertex("id123")` it knows to reference "vid" saving you from having to type it. – stephen mallette Feb 06 '14 at 16:53
  • Is there an `IdGraph` in 1.0? Not finding it for some reason. Thought I'd seen it before. – Dmitry Minkovsky Apr 04 '16 at 20:06
  • for TinkerPop 3.x you would use [ElementIdStrategy](http://tinkerpop.apache.org/docs/3.0.2-incubating/#_elementidstrategy) – stephen mallette Apr 04 '16 at 20:34
1

@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.

AVM
  • 303
  • 1
  • 4
  • 15