欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

janusgraph Composite Index创建 复合索引创建

程序员文章站 2024-01-19 13:25:46
...

janusgraph 索引创建需要一个过程,不像 Neo4j 中创建索引一样,一条语句就搞定

下面简单说明以下各个状态及操作:

索引状态

States(SchemaStatus) Description
INSTALLED The index is installed in the system but not yet registered with all instances in the cluster
REGISTERED The index is registered with all instances in the cluster but not (yet) enabled
ENABLED The index is enabled and in use (到这一步索引就可以用啦)
DISABLED The index is disabled and no longer in use (删除索引)

索引操作

Actions (SchemaAction) Description
REGISTER_INDEX Registers the index with all instances in the graph cluster. After an index is installed, it must be registered with all graph instances
REINDEX Re-builds the index from the graph(如果我们创建索引时已经存在数据,需要执行这个Action)
ENABLE_INDEX Enables the index so that it can be used by the query processing engine. An index must be registered before it can be enabled
DISABLE_INDEX Disables the index in the graph so that it is no longer used
REMOVE_INDEX Removes the index from the graph (optional operation). Only on composite index

官方文档创建索引步骤如下

graph.tx().rollback() //Never create new indexes while a transaction is active
mgmt = graph.openManagement()
name = mgmt.getPropertyKey('name')
age = mgmt.getPropertyKey('age')
mgmt.buildIndex('byNameComposite', Vertex.class).addKey(name).buildCompositeIndex()
mgmt.buildIndex('byNameAndAgeComposite', Vertex.class).addKey(name).addKey(age).buildCompositeIndex()
mgmt.commit()
//Wait for the index to become available
ManagementSystem.awaitGraphIndexStatus(graph, 'byNameComposite').call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byNameAndAgeComposite').call()
//Reindex the existing data
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex("byNameComposite"), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex("byNameAndAgeComposite"), SchemaAction.REINDEX).get()
mgmt.commit()

然而实际上会遇到很多问题
尤其是执行 ManagementSystem.awaitGraphIndexStatus(graph, 'byNameComposite').call() 的时候会报如下错误
Script evaluation exceeded the configured ‘scriptEvaluationTimeout’ threshold of 30000 ms or evaluation was otherwise cancelled directly for request [ManagementSystem.awaitGraphIndexStatus(graph, ‘byPerson_idComposite’).call()]
超时 即便把时间改长也会有
GraphIndexStatusReport[success=false, indexName='byPerson_idComposite', targetStatus=[REGISTERED], notConverged={person_id=ENABLED}, converged={}, elapsed=PT1M0.311S]
不成功

1.首先,创建索引之前,确定JanusGraph没有其它事务正在运行

查看事物命令 graph.getOpenTransactions()

gremlin> graph.getOpenTransactions()
==>standardjanusgraphtx[0x46d1d505]
==>standardjanusgraphtx[0x7def266a]
gremlin> graph.tx().rollback()
==>null
gremlin> graph.getOpenTransactions()
==>standardjanusgraphtx[0x46d1d505]
gremlin> graph.tx().rollback()
==>null
gremlin> graph.getOpenTransactions()
==>standardjanusgraphtx[0x46d1d505]
gremlin> for(i=0;i<1;i++) {graph.getOpenTransactions().getAt(0).rollback()}
==>null
gremlin> graph.getOpenTransactions()
gremlin>

可以看出 graph.tx().rollback() 命令是无法全部关闭的
正确的关闭方法:
for(i=0;i<size;i++) {graph.getOpenTransactions().getAt(0).rollback()} size替换为事务的数量

2.创建索引

mgmt = graph.openManagement()
person_id= mgmt.getPropertyKey('person_id')
mgmt.buildIndex('byPerson_idComposite', Vertex.class).addKey(name).buildCompositeIndex()
mgmt.commit()

3. 执行 REGISTER_INDEX ACTION,使索引状态INSTALLED 转为 REGISTERED

官方文档里没有这关键的一步,在创建完索引后,需要执行以下命令 注册索引

mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex('byPerson_idComposite'), SchemaAction.REGISTER_INDEX).get()
mgmt.commit()

此时我们可以通过下面命令查看索引状态是否变为 REGISTERED

gremlin> mgmt = graph.openManagement()
==>[email protected]61d
gremlin> index = mgmt.getGraphIndex('byPerson_idComposite')
==>byMovie_idComposite
gremlin> index.getIndexStatus(mgmt.getPropertyKey('person_id'))
==>REGISTERED

确定状态变为 REGISTERED 后再往下执行

4. 执行REINDEX与ENABLE_INDEX,完成索引

gremlin> mgmt = graph.openManagement()
==>[email protected]61d
gremlin> mgmt.updateIndex(mgmt.getGraphIndex('byPerson_idComposite'), SchemaAction.REINDEX).get()
==>org[email protected]1ca50889
gremlin> mgmt.commit()
gremlin> ManagementSystem.awaitGraphIndexStatus(graph, 'byPerson_idComposite').status(SchemaStatus.ENABLED).call()
==>GraphIndexStatusReport[success=true, indexName='byPerson_idComposite', targetStatus=[ENABLED], notConverged={}, converged={person_id=ENABLED}, elapsed=PT0.007S]
gremlin> 
==>null

可以看到索引已经 ENABLED

gremlin> mgmt = graph.openManagement()
==>[email protected]8d40
gremlin> index = mgmt.getGraphIndex('byPerson_idComposite')
==>byPerson_idComposite
gremlin> index.getIndexStatus(mgmt.getPropertyKey('person_id'))
==>ENABLED
gremlin> 

可以查看索引状态变为ENABLED 表示可以使用了

参考文章

上一篇: tf读取数据

下一篇: tf.data详解