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

问题描述 投票:0回答:1

我正在使用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()方法。我不确定我做得正确吗?

gremlin tinkerpop tinkerpop3 janusgraph gremlin-server
1个回答
0
投票

我认为您在混淆一些概念。如果要远程连接到Cluster实例,则只需要使用TinkerPop驱动程序(即Graph)。就您而言,您是在本地创建JanusGraph实例,因此只需执行graph.traversal()并开始编写Gremlin。另一方面,如果您将JanusGraph实例托管在Gremlin Server中,则需要使用withRemote()选项。如您所称的withRemote()弃用方式,但javadoc提到了新方法,该方法也可以在documentation中找到:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.