janusgraph index
查看index
- 首先shell的方式连接到janusgraph
启动bin/gremlin.sh,进入gremlin控制台 - 连接本地的gremlin server,
gremlin>:remote connect tinkerpop.server conf/remote.yaml
gremlin>:remote console - 打印索引情况
gremlin>mgmt = graph.openManagement();mgmt.printSchema();
几种index
graph index : composite index 和 minxed index
前者使用存储图数据的后端存储做索引存储,后者借助第三方的索引存储如elasticsearch
Composite indexes do not require configuration of an external indexing backend and are supported through the primary storage backend.
Composite indexes can also be used to enforce property uniqueness in the graph. If a composite graph index is defined as
unique()
there can be at most one vertex or edge for any given concatenation of property values associated with the keys of that index.
graph.tx().rollback() //Never create new indexes while a transaction is active
mgmt = graph.openManagement() name = mgmt.getPropertyKey('name') mgmt.buildIndex('byNameUnique', Vertex.class).addKey(name).unique().buildCompositeIndex() mgmt.commit() //Wait for the index to become available
ManagementSystem.awaitGraphIndexStatus(graph, 'byNameUnique').call() //Reindex the existing data
mgmt = graph.openManagement() mgmt.updateIndex(mgmt.getGraphIndex("byNameUnique"),SchemaAction.REINDEX).get()
mgmt.commit()
建议索引在导入数据前建立
The indexes should be created before data is loaded to avoid the need to re-index the graph, which is an expensive process.
在初始化部署数据库时建立composite索引,由于composite index是基于属性的索引,所以需要建立针对业务的一个属性obj_id,建索引的语句为下
mgmt = graph.openManagement();
objId =mgmt.makePropertyKey('obj_id').dataType(String.class).make();
VertexLabel nodeVertexLabel = mgmt.makeVertexLabel("node").make();
JanusGraphIndex objIdIndex = mgmt.buildIndex('byObjIdComposite', Vertex.class).addKey(objId).indexOnly(nodeVertexLabel).unique().buildCompositeIndex();
mgmt.setConsistency(objIdIndex, ConsistencyModifier.LOCK);
mgmt.commit();
如果数据库已经存在数据就需要reindex,这是耗时的一个过程,不建议这么做。