在 JanusGraph 中创建连接池和使用连接池

Creating Connection Pool and using the connection pool in JanusGraph

我正在使用 JanusGraph。如何在远程连接JanusGraph时创建连接池,然后使用池借用连接?

现在我正在做类似

的事情
private static void init() {
        String uri = "localhost";
        int poolSize = 5;

        graph = JanusGraphFactory.open("inmemory");
        cluster = Cluster.build()
                .addContactPoint(uri)
                .port(8182)
                .serializer(new GryoMessageSerializerV1d0(GryoMapper.build().addRegistry(JanusGraphIoRegistry.getInstance())))
                .maxConnectionPoolSize(poolSize)
                .minConnectionPoolSize(poolSize)
                .create();
        gts = graph
                .traversal()
                .withRemote(DriverRemoteConnection.using(cluster));
    }

这个init方法被初始化了一次。然后任何需要简单连接的人都可以调用下面的方法

public GraphTraversalSource getConnection() {
        return gts.clone();
    }

请注意 withRemote() 方法现已弃用。我不确定我这样做是否正确?

我认为您混淆了一些概念。如果您远程连接到 Graph 实例,则只需要使用 TinkerPop 驱动程序(即 Cluster)。在您的情况下,您正在本地创建 JanusGraph 实例,因此您只需执行 graph.traversal() 并开始编写 Gremlin。另一方面,如果您在 Gremlin 服务器中托管 JanusGraph 实例,则需要使用 withRemote() 选项。正如您在时尚中提到的 withRemote() 已弃用,但 javadoc mentions the new method which can also be found in the documentation

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));

要了解连接到 Graph 实例的所有不同选项,我建议阅读 this section 的 TinkerPop 参考文档。