如何从Java获取JanusGraphManagement

iibxawm4  于 2022-12-10  发布在  Java
关注(0)|答案(1)|浏览(102)

我不明白如何从使用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();

但它会产生下列例外状况:
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
}
.....

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

我想做的是直接从Java代码中定义图形模式,这就是为什么我需要一个管理示例和一个遍历源是不够的

8yparm6h

8yparm6h1#

他们似乎真的不希望您从Java中执行此操作。请检查我构建的示例repo的初始提交。
一般来说,这是一个内部的魔术。你需要为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))

不要忘记,您仍然需要首先创建模板配置。
现在,您可以在应用程序中的任何地方使用singleton 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)

编辑:

正如@FlorianHockmann在JanusGraph discord服务器上指出的那样,最好 * 不要 * 直接从Java中使用这些对象。

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()
看看我举的这个例子

相关问题