How to get JanusGraphManagement from Java
Asked Answered
S

1

6

I can't understand how to get a JanusGraphManagement instance from a graph created with the ConfiguredGraphFactory.

I tried doing something like this:

        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();

But it generates the following exception:

java.lang.NullPointerException: Gremlin Server must be configured to use the JanusGraphManager.

The gremlin-server is ruinning with the following configuration:

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
}
.....

And the JanusGraph's one is this:

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

What I'd like to do is to define the graph schema directly from Java code, that's why I need to the a managment instance and a traversal source is not enough

Stonecutter answered 22/8, 2019 at 15:35 Comment(0)
S
0

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))

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")

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)

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>()

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()

Just don't forget to call session.close() when you're done!

Check out this gist I made for an example

Stockton answered 7/12, 2022 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.