janusgraph Composite Index创建 复合索引创建
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 表示可以使用了
推荐阅读
-
janusgraph Composite Index创建 复合索引创建
-
janusgraph索引创建
-
janusgraph创建索引报错
-
Oracle 创建索引前估算索引大小(dbms_space.create_index_cost)
-
MySQL 创建索引(Create Index)的方法和语法结构及例子
-
MySQL 创建索引(Create Index)的方法和语法结构及例子
-
SQL Server 创建索引(index)
-
【ES】索引创建,为“非查询字段”不建索引 index store
-
Elasticsearch索引(index)、映射(mapping)等相关的创建
-
ElasticSearch创建索引(index)和添加映射(mapping)