如何删除JanusGraph索引?

5

索引状态已安装。如何将状态更改为已注册,然后禁用,以便删除它?

GraphTraversalSource g = janusGraph.traversal();
JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
JanusGraphIndex phoneIndex = janusGraphManagement.getGraphIndex("phoneIndex");
PropertyKey phone = janusGraphManagement.getPropertyKey("phone");
SchemaStatus indexStatus = phoneIndex.getIndexStatus(phone);
String name = phoneIndex.name();
System.out.println(name);
if (indexStatus == INSTALLED) {
    janusGraphManagement.commit();
    janusGraph.tx().commit();
}

image

3个回答

4
如果您无法将索引的状态Install更改为Enable,建议您检查JanusGraph的运行实例,并关闭除带有"(Current)"标识之外的所有实例,然后再次尝试删除该索引
使用以下命令在gremlin shell中检查和关闭实例:
    mgmt = graph.openManagement()

    mgmt.getOpenInstances() //all open instances

    ==>7f0001016161-dunwich1(current)
    ==>7f0001016161-atlantis1

    mgmt.forceCloseInstance('7f0001016161-atlantis1') //remove an instance
    mgmt.commit()

使用以下代码来删除索引:

   // Disable the "name" composite index
   this.management = this.graph.openManagement()
   def nameIndex = this.management.getGraphIndex(indexName)
   this.management.updateIndex(nameIndex, SchemaAction.DISABLE_INDEX).get()
   this.management.commit()
   this.graph.tx().commit()

   // Block until the SchemaStatus transitions from INSTALLED to REGISTERED
   ManagementSystem.awaitGraphIndexStatus(graph, indexName).status(SchemaStatus.DISABLED).call()
    
   // Delete the index using JanusGraphManagement
   this.management = this.graph.openManagement()
   def delIndex = this.management.getGraphIndex(indexName)
   def future = this.management.updateIndex(delIndex, SchemaAction.REMOVE_INDEX)
   this.management.commit()
   this.graph.tx().commit()

我遇到了同样的问题,尝试了很多其他方法,最终上述步骤起作用了!


这个方法会删除索引而不是禁用它吗?我想删除我的索引以创建另一个同名的索引,但是却收到错误消息:“已经定义了名称为'myIndexName'的索引”。 - Nimtar

2

必须先禁用Index,然后再removed

    // Disable the "phoneIndex" composite index
    janusGraphManagement = janusGraph.openManagement()
    phoneIndex = janusGraphManagement.getGraphIndex('phoneIndex')
    janusGraphManagement.updateIndex(phoneIndex, SchemaAction.DISABLE_INDEX).get()
    janusGraphManagement.commit()
    janusGraph.tx().commit()
    
    // Block until the SchemaStatus transitions from INSTALLED to REGISTERED
    ManagementSystem.awaitGraphIndexStatus(janusGraph, 'phoneIndex').status(SchemaStatus.DISABLED).call()
    
    // Delete the index using TitanManagement
    janusGraphManagement = janusGraph.openManagement()
    phoneIndex = janusGraphManagement.getGraphIndex('phoneIndex')
    future = janusGraphManagement.updateIndex(phoneIndex, SchemaAction.REMOVE_INDEX)
    janusGraphManagement.commit()
    janusGraph.tx().commit()

当我在JanusGraph上建立索引时,索引状态为“已安装”,无法转换为“启用”,因此索引状态为“已安装”,无法删除。 - lazyfighter

1

这里是完整的索引移除示例 (请参考此链接),使用 Java (与您的示例一样) 编写,并保存到 "inmemory" 中供您使用。
这样,您可以通过更改步骤来查看发生了什么,从而学习一个工作示例。

代码

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;
import org.janusgraph.core.PropertyKey;
import org.janusgraph.core.schema.*;
import org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics;
import org.janusgraph.graphdb.database.management.ManagementSystem;

import java.util.concurrent.ExecutionException;

public class Test {
    private static final Logger logger = LogManager.getLogger(Main.class);
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        JanusGraph janusGraph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
//        JanusGraph janusGraph = JanusGraphFactory.build().set("storage.backend", "cql").set("storage.hostname", "localhost:9042").open();
        GraphTraversalSource g = janusGraph.traversal();
        g.V().drop().iterate();
        janusGraph.tx().commit();
        JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
        PropertyKey propertyKey = janusGraphManagement.getOrCreatePropertyKey("_id");
//        if (!janusGraphManagement.containsGraphIndex("_id"))
        janusGraphManagement.buildIndex("_id", Vertex.class).addKey(propertyKey).buildCompositeIndex();
        janusGraphManagement.commit();
        JanusGraphVertex vertex = janusGraph.addVertex("entity");
        vertex.property("_id", "Test1");
        janusGraph.tx().commit();
        janusGraphManagement = janusGraph.openManagement();
        JanusGraphIndex janusGraphIndex = janusGraphManagement.getGraphIndex("_id");
        logger.info("index.getIndexStatus(propertyKey):\t" + janusGraphIndex.getIndexStatus(propertyKey));
        janusGraphManagement.updateIndex(janusGraphIndex, SchemaAction.DISABLE_INDEX).get();
        logger.info("index.getIndexStatus(propertyKey):\t" + janusGraphIndex.getIndexStatus(propertyKey));
        janusGraphManagement.commit();
        ManagementSystem.awaitGraphIndexStatus(janusGraph, "_id").status(SchemaStatus.DISABLED).call();
        logger.info("index.getIndexStatus(propertyKey):\t" + janusGraphIndex.getIndexStatus(propertyKey));
        janusGraphManagement = janusGraph.openManagement();
        janusGraphIndex = janusGraphManagement.getGraphIndex("_id");
        janusGraphManagement.updateIndex(janusGraphIndex, SchemaAction.DISCARD_INDEX).get();
        logger.info("index.getIndexStatus(propertyKey):\t" + janusGraphIndex.getIndexStatus(propertyKey));
        janusGraphManagement.commit();
        ManagementSystem.awaitGraphIndexStatus(janusGraph, "_id").status(SchemaStatus.DISCARDED).call();
        logger.info("index.getIndexStatus(propertyKey):\t" + janusGraphIndex.getIndexStatus(propertyKey));
        janusGraph.close();
    }
}

日志

Log4j2 的输出应该如下所示

2023-05-16 08:51:40,344 [INFO] [o.j.d.c.b.ReadConfigurationBuilder.main] ::  Set default timestamp provider MICRO
2023-05-16 08:51:40,355 [INFO] [o.j.g.i.UniqueInstanceIdRetriever.main] ::   Generated unique-instance-id=c0a8564911500-rmt-lap-win201
2023-05-16 08:51:40,364 [INFO] [o.j.d.c.ExecutorServiceBuilder.main] ::  Initiated fixed thread pool of size 40
2023-05-16 08:51:40,399 [INFO] [o.j.g.d.StandardJanusGraph.main] ::  Gremlin script evaluation is disabled
2023-05-16 08:51:40,403 [INFO] [o.j.d.l.k.KCVSLog.main] ::   Loaded unidentified ReadMarker start time 2023-05-16T13:51:40.403993Z into org.janusgraph.diskstorage.log.kcvs.KCVSLog$MessagePuller@255990cc
2023-05-16 08:51:40,460 [WARN] [o.j.g.t.StandardJanusGraphTx.main] ::    Query requires iterating over all vertices [[]]. For better performance, use indexes
2023-05-16 08:51:40,791 [INFO] [Main.main] ::    index.getIndexStatus(propertyKey): ENABLED
2023-05-16 08:51:40,801 [INFO] [Main.main] ::    index.getIndexStatus(propertyKey): DISABLED
2023-05-16 08:51:40,919 [INFO] [o.j.g.d.m.GraphIndexStatusWatcher.main] ::   All 1 key(s) on index _id have status(es) [DISABLED]
2023-05-16 08:51:40,919 [INFO] [Main.main] ::    index.getIndexStatus(propertyKey): DISABLED
2023-05-16 08:51:40,952 [INFO] [o.j.g.o.j.IndexRemoveJob.Thread-3] ::    Index _id metrics: success-tx: 4 doc-updates: 0 succeeded: 1
...
2023-05-16 08:51:41,169 [INFO] [o.j.g.d.m.ManagementSystem.Thread-1] ::  Index update job successful for [_id]
2023-05-16 08:51:41,175 [INFO] [Main.main] ::    index.getIndexStatus(propertyKey): DISCARDED
2023-05-16 08:51:41,178 [INFO] [o.j.g.d.m.GraphIndexStatusWatcher.main] ::   All 1 key(s) on index _id have status(es) [DISCARDED]
2023-05-16 08:51:41,178 [INFO] [Main.main] ::    index.getIndexStatus(propertyKey): DISCARDED

Process finished with exit code 0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接