janusgraph的java远程遍历

bqjvbblv  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(424)

我正在构建一个java应用程序,它需要连接到一个远程janusgraph服务器并动态创建图形。
我已经安装/配置了带有berkeley数据库后端和configurationmanagementgraph支持的单节点janusgraph服务器,以便可以在服务器上创建/管理多个图形。
在gremlin控制台中,我可以连接到远程服务器、创建图形、创建顶点等。示例:

gremlin> :remote connect tinkerpop.server conf/remote.yaml session
gremlin> :remote console

gremlin> map = new HashMap<String, Object>();
gremlin> map.put("storage.backend", "berkeleyje");
gremlin> map.put("storage.directory", "db/test");
gremlin> ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map));
gremlin> ConfiguredGraphFactory.create("test");

gremlin> graph = ConfiguredGraphFactory.open("test");
gremlin> g = graph.traversal();
gremlin> g.addV("person").property("name", "peter");
gremlin> g.tx().commit();
gremlin> graph.vertices().size();
==>1
gremlin> g.V();
==>v[4288]
gremlin> g.V().count();
==>1
gremlin> g.close();

到现在为止,一直都还不错。在java方面,我可以连接到远程服务器并通过client.submit()方法发出命令。在下面的示例中,我连接到远程服务器并创建一个名为“test2”的新图形:

Cluster cluster = Cluster.build()
    .addContactPoint(host)
    .port(port)
    .serializer(Serializers.GRYO_V3D0)
    .create();

String name = "test2";

String sessionId = UUID.randomUUID().toString();
Client client = cluster.connect(sessionId);
client.submit("map = new HashMap<String, Object>();");
client.submit("map.put(\"storage.backend\", \"berkeleyje\");");
client.submit("map.put(\"storage.directory\", \"db/" + name + "\");");
client.submit("ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map));");
client.submit("ConfiguredGraphFactory.create(\"" + name + "\");");

我可以使用client.submit()方法确认已创建图形,并以编程方式查看其他图形:

ResultSet results = client.submit("ConfiguredGraphFactory.getGraphNames()");
Iterator<Result> it = results.iterator();
while (it.hasNext()){
    Result result = it.next();
    String graphName = result.getString();
    System.out.println(graphName);
}

接下来,我要连接到一个图并以编程方式遍历节点(在java中)。然而,我似乎不知道该怎么做。据我所知,应该是这么简单:

DriverRemoteConnection conn = DriverRemoteConnection.using(client, name); //"_traversal"
GraphTraversalSource g = AnonymousTraversalSource.traversal().withRemote(conn);

这些命令不会引发任何错误,但graphtraversalsource似乎为空:

System.out.println(g.getGraph()); //always returns emptygraph[empty]
System.out.prinltn(g.V()); //Appears to be empty [GraphStep(vertex,[])]
Iterator<Vertex> it = g.getGraph().vertices(); //empty

关于如何在java中为远程janusgraph服务器获取graphtraversalsource有什么建议吗?我怀疑我的问题与configurationmanagementgraph有关,但我不能对此置之不理。同样,client.submit()可以工作。如果我能做这样的事就好了:

GraphTraversalSource g = (GraphTraversalSource) client.submit("ConfiguredGraphFactory.open(\"" + name + "\");").iterator().next();

……但这当然行不通
更新
查看代码,似乎忽略了传递给driverremoteconnection的图形名称(remotetraversalsourcename)。
从driverremoteconnection开始:

DriverRemoteConnection conn = DriverRemoteConnection.using(client, name);

在后台,图形名称(remotetraversalsourcename)只是用来设置别名(例如client.alias(name);)
接下来,在anonymoustraversalsource.traversal().withremote()方法中

GraphTraversalSource g = AnonymousTraversalSource.traversal().withRemote(conn);

在引擎盖下,withremote()正在调用:

traversalSourceClass.getConstructor(RemoteConnection.class).newInstance(remoteConnection);

其中traversalsourceclass是graphtraversalsource.class
与此相同:

g = GraphTraversalSource.class.getConstructor(RemoteConnection.class).newInstance(conn);

最后,graphtraversalsource的构造函数如下所示:

public GraphTraversalSource(final RemoteConnection connection) {
    this(EmptyGraph.instance(), TraversalStrategies.GlobalCache.getStrategies(EmptyGraph.class).clone());
    this.connection = connection;
    this.strategies.addStrategies(new RemoteStrategy(connection));
}

如您所见,graphtraversalsource中的graph变量从未设置。
我怀疑(a)我不应该使用anonymoustraversalsource,或者(b)我需要以其他方式示例化graphtraversalsource,也许是使用一个graph对象。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题