Pis*_*hio 5 java gremlin-server janusgraph
我无法理解如何从使用ConfiguredGraphFactory 创建的图表中获取JanusGraphManagement 实例。
我尝试做这样的事情:
JanusGraphFactory.Builder config = JanusGraphFactory.build();
config.set("storage.hostname", storageHostname);
config.set("storage.port", storagePort);
config.set("storage.backend", STORAGE_BACKEND);
config.set("index.search.backend", SEARCH_BACKEND);
config.set("index.search.hostname", indexHostname);
config.set("index.search.port", indexPort);
config.set("graph.graphname", graphName);
JanusGraph graph = config.open();
JanusGraphManagement mgmt = graph.openManagement();
Run Code Online (Sandbox Code Playgroud)
但它会产生以下异常:
java.lang.NullPointerException:必须将 Gremlin 服务器配置为使用 JanusGraphManager。
gremlin-server 因以下配置而崩溃:
host: 0.0.0.0
port: 8182
scriptEvaluationTimeout: 180000
# channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer
channelizer: org.janusgraph.channelizers.JanusGraphWebSocketChannelizer
graphManager: org.janusgraph.graphdb.management.JanusGraphManager
graphs: {
#graph: conf/gremlin-server/janusgraph-cql-es-server.properties,
ConfigurationManagementGraph: conf/gremlin-server/janusgraph-cql-es-server-configured.properties
}
.....
Run Code Online (Sandbox Code Playgroud)
JanusGraph 的一个是这样的:
gremlin.graph=org.janusgraph.core.ConfiguredGraphFactory
graph.graphname=ConfigurationManagementGraph
storage.backend=cql
storage.hostname=127.0.0.1
storage.cql.keyspace=janusgraph
cache.db-cache = true
cache.db-cache-time = 180000
cache.db-cache-size = 0.25
index.search.backend=elasticsearch
index.search.hostname=127.0.0.1
index.search.elasticsearch.client-only=true
Run Code Online (Sandbox Code Playgroud)
我想做的是直接从Java代码定义图形模式,这就是为什么我需要管理实例并且遍历源是不够的
They really don't seem to want you to do this from Java. Check my initial commit to an example repo I built.
The general deal is that there is a bunch of internal magic happening. You need to make a new embedded instance of the ConfigurationManagementGraph and a few other things. The steps to get ConfiguredGraphFactory up and running are:
JanusGraphManager(Settings())
// the configuration file you used for your ConfigurationManagementGraph in your `janusgrpah-server.yaml` file
val mgrConfFile = File("conf/janusgraph-cql-configurationgraph.properties")
// load the configuration
val base = CommonsConfiguration(ConfigurationUtil.loadPropertiesConfig(mgrConfFile))
// modify a fe wthings specific to the ConfigurationManagementGraph
base.set("graph.graphname", "name-of-this-graph-instance")
base.set("graph.unique-instance-id", "some-super-unique-id")
base.set("storage.lock.local-mediator-group", "tmp")
// duplicate the config for some reason?
val local = ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, base, BasicConfiguration.Restriction.NONE)
// build another type of configuration?
val config = GraphDatabaseConfiguration(base, local, instanceId, local)
// create the new ConfigurationManagementGraph instance
return ConfigurationManagementGraph(StandardJanusGraph(config))
Run Code Online (Sandbox Code Playgroud)
Don't forget that you will still need to create a template configuration first.
Now, you can use the singleton ConfiguredGraphFactory anywhere in your application, just like the docs say.
val myGraph = ConfiguredGraphFactory.open("myGraph")
Run Code Online (Sandbox Code Playgroud)
Keep in mind that you may not need to do this. The Client.submit() function comes in handy for most things. For example:
// connect to the gremlin server
val cluster = Cluster.build("localhost").create()
val client = cluster.connect<Client.ClusteredClient>()
// example: get a list of existing graph names
val existingGraphs = client.submit("ConfiguredGraphFactory.getGraphNames()").all().get()
// check if a graph exists
val exists = existingGraphs.any { it.string == "myGraph" }
// create a new graph with the existing template
// (note: this *cannot* be cast to a JanusGraph, even though that would make this really useful)
val myGraph: TinkerGraph = client.submit("ConfiguredGraphFactory.getGraphNames()").all().get().first().get(TinkerGraph::class.java)
Run Code Online (Sandbox Code Playgroud)
EDIT:
As @FlorianHockmann pointed out on the JanusGraph discord server, it's preferable to not use these objects directly from your Java. Instead, it's better to use a Client.SessionedClient when you connect, like so
val cluster = Cluster.build("localhost").create()
val session = cluster.connect<Client.SessionedClient>()
Run Code Online (Sandbox Code Playgroud)
Since you've established a session, you can now save and re-use variables on the server. As Florian put it,
client.submit("mgmt = ConfiguredGraphFactory.open('myGraph').openManagement()").all().get()
// afterwards you can use it:
client.submit("// do stuff with mgmt").all().get()
Run Code Online (Sandbox Code Playgroud)
Just don't forget to call session.close() when you're done!
Check out this gist I made for an example
| 归档时间: |
|
| 查看次数: |
393 次 |
| 最近记录: |