0

Using the following commands to use indexes for better performance to query a node in titan db .

TitanManagement mgmt = graph.openManagement();
PropertyKey buyer = mgmt.makePropertyKey("buyer").dataType(String.class).cardinality(Cardinality.SINGLE).make();
TitanGraphIndex buyeri = mgmt.buildIndex("buyer", Vertex.class).addKey(buyer).buildCompositeIndex();
mgmt.setConsistency(buyeri, ConsistencyModifier.LOCK);
g.V().has("buyer","buyer", "buyer10").out("order_is").values("order").fill(list);     

Using titan 1.0.0 , gremlin query language, while running this query it throws an error:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)Caused by: com.thinkaurelius.titan.core.SchemaViolationException: Adding this property for key [~T$SchemaName] and value [rtbuyer] violates a uniqueness constraint [SystemIndex#~T$SchemaName]
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.addProperty(StandardTitanTx.java:780)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.addProperty(StandardTitanTx.java:706)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.makeSchemaVertex(StandardTitanTx.java:836)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.makePropertyKey(StandardTitanTx.java:856)
at com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker.make(StandardPropertyKeyMaker.java:86)
at pluradj.titan.tinkerpop3.example.JavaExample2.main(JavaExample2.java:56)

Updates as mentioned below in @jason Plurad answer

I have used

 PropertyKey buyer = (!mgmt.containsPropertyKey("buyer")) ?
        mgmt.makePropertyKey("buyer").dataType(String.class).cardinality(Cardinality.SINGLE).make() :
        mgmt.getPropertyKey("buyer");
    TitanGraphIndex buyeri = mgmt.getGraphIndex("buyeri");
    if (buyeri == null) {
        VertexLabel buyr = mgmt.getVertexLabel("buyer");
        buyeri = mgmt.buildIndex("buyeri", Vertex.class).addKey(buyer).indexOnly(buyr). buildCompositeIndex();
        mgmt.setConsistency(buyeri, ConsistencyModifier.LOCK);
    }

    mgmt.commit();
     st=System.currentTimeMillis();
     g.V().has("buyer","buyer", "buyer10").out("order_is").values("order").fill(list);     
     System.out.println(System.currentTimeMillis()-st +"millllli");

Using the following code to index buyer , to make searching vertex faster , but dont know why it is not indexing is not working , Can anyone fix this please.

i have read the documentation of titan db indexing part but i guess it is not working ..

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
user3646858
  • 127
  • 1
  • 2
  • 11

1 Answers1

5

The problem here is indicated by this in the stack trace:

Caused by: com.thinkaurelius.titan.core.SchemaViolationException: Adding this property for key [~T$SchemaName] and value [rtbuyer] violates a uniqueness constraint [SystemIndex#~T$SchemaName]

Your code is attempting to create the schema multiple times (perhaps by repeated invocations of your program). Your schema creation code above does not check whether the property key exists before attempting to create it.

Your usage of has("buyer", "buyer", "buyer10") is looking for vertices that have a vertex label "buyer", and a vertex property "buyer" equal to "buyer10". Your schema definition definition didn't have a vertex label, so I think has("buyer", "buyer10") is more appropriate.

Check out this example that you can run from gremlin.sh. It uses the index and does not show any warning messages.

gremlin> graph = TitanFactory.build().set('storage.backend','inmemory').open()
==>standardtitangraph[inmemory:[127.0.0.1]]
gremlin> g = graph.traversal()
==>graphtraversalsource[standardtitangraph[inmemory:[127.0.0.1]], standard]
gremlin> mgmt = graph.openManagement()
==>com.thinkaurelius.titan.graphdb.database.management.ManagementSystem@7026b7ee
gremlin> buyer = (!mgmt.containsPropertyKey("buyer")) ?
gremlin>     mgmt.makePropertyKey("buyer").dataType(String.class).cardinality(Cardinality.SINGLE).make() :
gremlin>     mgmt.getPropertyKey("buyer");
==>buyer
gremlin> buyeri = mgmt.getGraphIndex("buyeri");
==>null
gremlin> if (buyeri == null) {
gremlin>     buyeri = mgmt.buildIndex("buyeri", Vertex.class).addKey(buyer).buildCompositeIndex();
gremlin>     mgmt.setConsistency(buyeri, ConsistencyModifier.LOCK);
gremlin> }
==>null
gremlin> mgmt.commit();
==>null
gremlin> v = graph.addVertex("buyer", "buyer10", "name", "ten")
==>v[4184]
gremlin> g.V().has("buyer", "buyer10").values("name")
==>ten
Jason Plurad
  • 6,422
  • 2
  • 15
  • 37
  • g.V().has("buyer","buyer", "buyer10").out("order_is").values("order").fill(list);I am using this command after mgmt.commit();But Still in terminal it is showing : iterating over all vertices [(~label = buyer AND buyer = buyer10)]. For better performance, use indexes. It means it is not using indexes at all hence slow speed. – – user3646858 Oct 05 '15 at 09:07
  • Please Reply :( @Jason Plurad – user3646858 Oct 05 '15 at 11:14
  • updated my answer with a full example you can run in gremlin console – Jason Plurad Oct 05 '15 at 13:28
  • Its still showing warning iterating over all vertices [(buyer = buyer10)]. For better performance, use indexes – user3646858 Oct 05 '15 at 13:48
  • Using Java Eclipse @JasonPlurad – user3646858 Oct 05 '15 at 14:02
  • You wont get index warning in gremlin shell , but if you run from Java Eclipse , g.V().has("buyer","buyer10") .values("buyer").fill(list); LOGGER.info(list.toString()); , It will show index warnings – user3646858 Oct 05 '15 at 14:18
  • Then I imagine there's something wrong with your Java Eclipse environment. Do you have a full code example you can share? – Jason Plurad Oct 05 '15 at 14:20
  • one more https://drive.google.com/file/d/0BxBLXfahBsXzbzFRdlRmVzU2Nkk/view?usp=sharing – user3646858 Oct 05 '15 at 14:28
  • You need to define your schema before you load the data. The default settings in Titan will auto-create a schema for you otherwise. http://s3.thinkaurelius.com/docs/titan/1.0.0/schema.html#_automatic_schema_maker This explains why you were running into the initial error when attempting to define the schema. – Jason Plurad Oct 05 '15 at 14:36
  • Move the call to `graph.io(graphson().readGraph()` after the schema definition -- i.e. after `mgmt.commit()` – Jason Plurad Oct 05 '15 at 15:03