Gao*_*Gao 7 tinkerpop janusgraph
我想使用Java API来操作远程服务器上的图形,服务器实际上在localhost中托管.我用来连接服务器的代码是:
JanusGraphFactory.Builder b = JanusGraphFactory.build();
b.set("hosts", "[localhost]");
JanusGraph graph = b.open();
Run Code Online (Sandbox Code Playgroud)
但是在我运行程序之后,它抛出了这样的异常:
线程"main"中的异常java.lang.IllegalStateException:需要设置配置值:root.storage.backend
那么如何使用Java API连接到远程JanusGraph服务器?
小智 8
我发现的文档建议创建一个EmtpyGraph并从中获取远程遍历:
EmptyGraph.instance().traversal().withRemote(config);
Run Code Online (Sandbox Code Playgroud)
其中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");
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了使用JanusGraph特定功能(例如提交事务)的问题,因为图形实例是EmptyGraph本身,而不是JanusGraph.所以,试试:
GraphTraversalSource g = JanusGraphFactory.open("inmemory").traversal().withRemote(config);
Run Code Online (Sandbox Code Playgroud)
这将为您提供远程gremlin-server的遍历源,您可以执行g.addV("vertexLabel").... ,g.tx().commit(); 等等.
这是一个简单的方法:
graph = JanusGraphFactory.open("conf/janusgraph-cassandra-solr.properties")
juno = graph.addVertex() //Automatically opens a new transaction
juno.property("name", "juno")
graph.tx().commit() //Commits transaction
Janus 使用现有的存储解决方案(如 cassandra、hbase、berkelydb)来存储数据。
您可以通过 2 种方式进行连接: 1 - 连接到远程 gremlin 服务器并远程执行遍历/查询。这是通过使用tinkerpop gremlin 2 的 Cluster 和 EmptyGraph - 使用我在上面的帖子中建议的技术直接从您的应用程序连接。
连接到远程 gremlin 服务器的优点/缺点
优点
缺点
通过直接从客户端代码连接(避免远程连接),您可以获得更多控制权。
此外,您可以直接在代码中使用 JanusGraph 实例(这仍然是 gremlin 的抱怨),以充分利用 JanusGraph API。
大多数答案现在都已经过时了。
要远程连接任何 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();
}
Run Code Online (Sandbox Code Playgroud)
GraphTraversalSource
是一个线程安全的对象,理想情况下应该是一个单例。
每个graphTraversalSource
对象都将其连接池保留在其上下文中。
小智 2
尝试这个:
JanusGraphFactory.Builder builder = JanusGraphFactory.build().
set("storage.hostname", "localhost").
set('storage.backend', 'cassandra') //or whatever you are using as backend
builder.open();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4084 次 |
最近记录: |