如何远程连接到JanusGraph服务器?

14

我想使用Java API来操作远程服务器上的图形,实际上该服务器托管在本地主机上。我用来连接服务器的代码是:

JanusGraphFactory.Builder b = JanusGraphFactory.build();
b.set("hosts", "[localhost]");
JanusGraph graph = b.open();

但是当我运行程序后,它抛出了这样的异常:

Exception in thread "main" java.lang.IllegalStateException: Need to set configuration value: root.storage.backend

那么我该如何使用Java API连接到远程的JanusGraph服务器?

6个回答

9
我找到的文档建议创建一个EmptyGraph,然后从该图形获取一个远程遍历:
EmptyGraph.instance().traversal().withRemote(config);

其中config是一个包含远程属性的配置对象,例如:

    config.setProperty("clusterConfiguration.hosts", HOST);
    config.setProperty("clusterConfiguration.port", PORT);
    config.setProperty("clusterConfiguration.serializer.className", "org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0");
    config.setProperty("clusterConfiguration.serializer.config.ioRegistries", ioRegistries); // (e.g. [ org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry) ]
    config.setProperty("gremlin.remote.remoteConnectionClass", "org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection");
    config.setProperty("gremlin.remote.driver.sourceName", "g");

然而,我在使用 JanusGraph 特定功能(例如提交事务)时遇到了问题,因为图实例本身是 EmptyGraph,而不是 JanusGraph。所以,请尝试:

GraphTraversalSource g = JanusGraphFactory.open("inmemory").traversal().withRemote(config);

这将为您提供遍历远程gremlin服务器的数据源,您可以执行g.addV("vertexLabel")....;、g.tx().commit();等操作。

1
你能举个例子来说明如何传递 ioRegistries 的值吗? - drum
1
基本上存在一个bug,如果您只提供一个String列表,则ioRegistries值无法正常工作。我目前正在使用Arrays.asList("org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry","org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry")进行解决,但我还没有完全测试它。 - Samy Elaiassi
我该如何连接到已经存在于服务器上的特定图形? - Sagar Vaghela

5

以下是一种简单的方法:

graph = JanusGraphFactory.open("conf/janusgraph-cassandra-solr.properties") juno = graph.addVertex() //自动打开新事务 juno.property("name", "juno") graph.tx().commit() //提交事务

Janus用户文档

Janus使用现有的存储解决方案,如cassandra、hbase、berkelydb来存储数据。

您可以通过2种方式连接: 1-连接到远程gremlin服务器并远程执行遍历/查询。这是使用tinkerpop gremlin的Cluster和EmptyGraph实现的。 2-直接从您的应用程序连接,使用我在上面发帖中提出的技术。

连接到远程gremlin服务器的优缺点

优点

  • 服务器具有更多控制权,并且所有查询都是集中处理的。
  • 由于每个人都通过远程gremlin服务器运行遍历/查询,因此所有内容都受到事务保护。默认情况下,远程gremlin服务器在事务中运行遍历/查询。
  • 中央策略管理
  • 中央模式管理

缺点

  • 手动事务管理很困难
  • 您需要将Groovy脚本作为字符串使用,并将其发送到remove(集群提交)以进行代码的事务执行。

通过直接从客户端代码连接(避免远程连接),您可以获得更多控制权。

此外,您可以直接在代码中使用JanusGraph实例,该实例仍然符合Gremlin规范,以充分利用JanusGraph API。


1
如何在“远程 gremlin 服务器和远程执行遍历/查询”的方式中进行“手动事务管理”?我似乎找不到任何示例。请问您能否提供一个示例?谢谢。 - Tin Ng
1
链接失效(404) - Ben Fulton
你好,能否提供一个远程JanusGraph事务的例子?如果有的话,请提供。我无法在远程上正确地执行回滚操作。 - Serhii Zadorozhnyi

5

大多数答案现在已经过时。

要远程连接任何TinkerPop3图形数据库,我们需要创建一个集群对象。 使用此集群对象,我们可以获取 graphTraversalSource。 当程序结束时,为释放连接池,这两个对象都需要关闭。

    private static Cluster cluster;
    private static GraphTraversalSource gts;

    private static void init() {
        cluster = Cluster.build()
                .addContactPoint(uri)
                .port(port)
                .serializer(Serializers.GRYO_V3D0)
                .maxInProcessPerConnection(32)
                .maxSimultaneousUsagePerConnection(32)
                .maxContentLength(10000000)
                .maxWaitForConnection(10)
                .minConnectionPoolSize(poolSize)
                .maxConnectionPoolSize(poolSize+5)
                .create();

        gts = AnonymousTraversalSource
                .traversal()
                .withRemote(DriverRemoteConnection.using(cluster));
    }


    public GraphTraversalSource getConnection() {
        return gts;
    }

    public static void finalise() throws Exception {
        gts.close();
        cluster.close();
    }

GraphTraversalSource 是一个线程安全的对象,理想情况下应该是单例的。
每个 graphTraversalSource 对象都在其上下文中保留其连接池。


0.6.2 开始,我必须像这样将一个完整的对象传递给 .serializer(...) 参数:MessageSerializer<?> serializer = Serializers.GRAPHBINARY_V1D0.simpleInstance(); serializer.configure(Map.of("ioRegistries", List.of("org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry")), Map.of()); - Lance

2

试试这个:

JanusGraphFactory.Builder builder = JanusGraphFactory.build().
            set("storage.hostname", "localhost").
            set('storage.backend', 'cassandra') //or whatever you are using as backend
builder.open();

我该如何使用Gremlin的标准API连接它? - Gao
为此,您需要配置remote.yaml文件并提供以下信息: hosts: [server-address] port: [port] serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }}然后您可以使用以下代码进行连接:import org.apache.tinkerpop.gremlin.driver.Client; import org.apache.tinkerpop.gremlin.driver.Cluster; Cluster cluster = Cluster.open("conf/remote.yaml"); Client client = cluster.connect(); client.submit("graph.addVertex(T.label,'x','name','tom')"); - Mansha Chuttani
以下错误提示:Gremlin Server 必须配置为使用 JanusGraphManager。 - Sagar Vaghela

1

请查看janusgraph示例中的RemoteGraph

RemoteGraphApp类下,您可以找到如何连接远程janusgraph服务器(集群)的方法。

 conf = new PropertiesConfiguration(propFileName);

    // using the remote driver for schema
    try {
        cluster = Cluster.open(conf.getString("gremlin.remote.driver.clusterFile"));
        client = cluster.connect();
    } catch (Exception e) {
        throw new ConfigurationException(e);
    }

    // using the remote graph for queries
    graph = EmptyGraph.instance();
    g = graph.traversal().withRemote(conf);

集群配置文件包含以下内容:

 hosts: [127.0.0.1]
 port: 8182
 serializer: {
   className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0,
   config: {
    ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry]
   } 
 }

0
请使用以下代码:
        JanusGraphFactory.Builder config = JanusGraphFactory.build();
        config.set("storage.backend", "cassandrathrift");
        config.set("storage.cassandra.keyspace", "graph1");
        config.set("storage.hostname", "127.0.0.1");

        JanusGraph graph = config.open();

以下是错误信息。 Gremlin Server 必须配置为使用 JanusGraphManager。 - Sagar Vaghela
检查您本地 pom.xml 文件和远程服务器上的 JanusGraph 版本。两者应该具有相同的版本。 - KayV

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