他们似乎真的不希望您从 Java 中执行此操作。检查我的对我构建的示例存储库的初始提交 https://github.com/baughmann/janusgraph-configuredgraphfactory-java-demo/blob/7c4f89882d8c50280a3e78556384dee88e0bdd22/janusgraph-configuredgraphfactory-demo/src/main/kotlin/Main.kt#L29.
一般来说,有很多内部魔法正在发生。您需要创建一个新的嵌入实例ConfigurationManagementGraph
以及其他一些事情。获取步骤ConfiguredGraphFactory
启动并运行的是:
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))
不要忘记您仍然需要创建一个先模板配置 https://docs.janusgraph.org/operations/configured-graph-factory/#template-configuration.
现在,您可以使用单例ConfiguredGraphFactory
就像文档所说的那样,在应用程序的任何位置。
val myGraph = ConfiguredGraphFactory.open("myGraph")
请记住,您可能不需要这样做。这Client.submit()
函数对于大多数事情都很方便。例如:
// 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)
EDIT:
正如 @FlorianHockmann 在 JanusGraph 不和谐服务器上指出的那样,最好not直接从 Java 中使用这些对象。相反,最好使用Client.SessionedClient
当你连接时,就像这样
val cluster = Cluster.build("localhost").create()
val session = cluster.connect<Client.SessionedClient>()
由于您已经建立了会话,因此您现在可以在服务器上保存和重复使用变量。正如弗洛里安所说,
client.submit("mgmt = ConfiguredGraphFactory.open('myGraph').openManagement()").all().get()
// afterwards you can use it:
client.submit("// do stuff with mgmt").all().get()
只是别忘了打电话session.close()
当你完成时!
看看我做的这个要点作为例子