使用以下命令使用索引以获得更好的性能来查询 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$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)
如下@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 indexing part的文档,但我想它不起作用..