janusgraph Composite Index创建 复合索引创建

本文详细介绍了JanusGraph中索引创建的过程,包括索引的状态与操作,解决索引创建过程中常见的问题,如超时错误和事务冲突,以及如何正确地创建和启用复合索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

下面简单说明以下各个状态及操作:
索引状态
States(SchemaStatus)Description
INSTALLEDThe index is installed in the system but not yet registered with all instances in the cluster
REGISTEREDThe index is registered with all instances in the cluster but not (yet) enabled
ENABLEDThe index is enabled and in use (到这一步索引就可以用啦)
DISABLEDThe index is disabled and no longer in use (删除索引)
索引操作
Actions (SchemaAction)Description
REGISTER_INDEXRegisters the index with all instances in the graph cluster. After an index is installed, it must be registered with all graph instances
REINDEXRe-builds the index from the graph(如果我们创建索引时已经存在数据,需要执行这个Action)
ENABLE_INDEXEnables the index so that it can be used by the query processing engine. An index must be registered before it can be enabled
DISABLE_INDEXDisables the index in the graph so that it is no longer used
REMOVE_INDEXRemoves 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(person_id).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()
==>org.janusgraph.graphdb.database.management.ManagementSystem@e2d061d
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()
==>org.janusgraph.graphdb.database.management.ManagementSystem@e2d061d
gremlin> mgmt.updateIndex(mgmt.getGraphIndex('byPerson_idComposite'), SchemaAction.REINDEX).get()
==>org.janusgraph.diskstorage.keycolumnvalue.scan.StandardScanMetrics@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()
==>org.janusgraph.graphdb.database.management.ManagementSystem@79718d40
gremlin> index = mgmt.getGraphIndex('byPerson_idComposite')
==>byPerson_idComposite
gremlin> index.getIndexStatus(mgmt.getPropertyKey('person_id'))
==>ENABLED
gremlin> 

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

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

viziviuz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值