java 如何删除JanusGraph索引?

ruoxqz4g  于 2023-05-21  发布在  Java
关注(0)|答案(3)|浏览(107)

Index status is installed .如何更改状态为registered,然后disabled,到remove呢?

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();
}

cqoc49vn

cqoc49vn1#

如果您无法将status of indexInstall更改为Enable,我建议您检查JanusGraph的运行示例,并关闭所有示例,除了带有“(Current)”的示例,然后是try again removing the index
要检查和关闭示例,请在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()

我面对同样的问题,并尝试了很多其他的东西,然后终于上述程序的工作!

eyh26e7m

eyh26e7m2#

必须先禁用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()
az31mfrm

az31mfrm3#

下面是用Java (as with your example)编码并保存到"inmemory"a full index-removal example
这样你就可以看到一个工作的例子,通过改变步骤来学习,看看会发生什么。

代码

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

相关问题