尝试在 titan graph db 中使用索引时出错

Error while Trying to use indexes in titan graph db

使用以下命令使用索引以获得更好的性能来查询 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);     

使用 titan 1.0.0 gremlin 查询语言,而 运行 此查询会抛出错误:

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

更新如下@jason Plurad 回答

我用过

 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");

使用下面的代码索引买家,使搜索顶点更快,但不知道为什么它不索引不起作用,谁能解决这个问题。

我已经阅读了 titan db 索引部分的文档,但我想它不起作用..

此处的问题在堆栈跟踪中由以下指示:

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

您的代码多次尝试创建模式(可能是通过重复调用您的程序)。您上面的模式创建代码在尝试创建之前不会检查 属性 键是否存在。

您对 has("buyer", "buyer", "buyer10") 的用法是寻找具有顶点标签 "buyer" 和顶点 属性 "buyer" 等于 "buyer10" 的顶点。您的模式定义定义没有顶点标签,所以我认为 has("buyer", "buyer10") 更合适。

查看此示例,您可以从 gremlin.sh 运行。它使用索引并且不显示任何警告消息。

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