Infinite thanks to @FlorianHockmann on the JanusGraph Discord server for his help! I knew I had to be doing something wrong in my first iteration.
I had the goal of programmatically working with ConfiguredGraphFactory
to create separate graphs for the purposes of multitenancy. Since I was new to JanusGraph and the docs for ConfiguredGraphFactory
didn't give a Java example, I scoured the internet and the JanusGraph source for options.
With help from @FlorianHockmann on the JanusGraph discord, I was able to modify this example to use only the Gremlin Driver, helping to keep the application more Gremlin-implementation-agnostic (though still not entirely).
As per usual, I was overthinking it. The basic workflow is:
- Setup the configuration files
janusgraph-cql-configurationgraph.properties
: The configuration file for the management graph (graph of graphs)janusgraph-template.properties
: The base template for new user graphsjanusgraph-server.yaml
: The Gremlin server config specifying the ConfigurationManagementGraph to be created by default and the Groovy scripts to run to create the user graph template
- Setup the Groovy scripts
configure-graph-template.groovy
: A Groovy script to create the user graph template if it has not already been loadedempty-sample.groovy
: The default Groovy script included with JanusGraph. Some pieces need to be commented out to avoid throwing errors, or the reference to it needs to be removed from thejanusgraph-server.yaml
- In Java/Kotlin/Scala:
- Connect to the server using Gremlin
Cluster
API - Submit a Groovy script to JanusGraph's Gremlin server to create the graph using the Gremlin
Client
API - Open the graph using
withRemote()
- Use the traversal as you would normally
- Connect to the server using Gremlin
File | Description |
---|---|
docker/conf/janusgraph-cql-configurationgraph.properties |
The configuration file for the Graph Management Graph--the graph that contains metadata about all of the graphs inside the JanusGraph database |
docker/conf/janusgraph-template.properties |
The configuration file used as a template for creating new graphs (besides the management graph) |
docker/conf/janusgraph-server.yaml |
The configuration file for the gremlin server (I think). This file includes the changes outlined in the JanusGraph docs |
scrips/empty-sample.groovy |
Derfault Groovy script used by janusgraph to set the global g vaiable. Obviously, we cannot use this since there is no default graph. It throws an error if you leave it in. |
scripts/configure-graph-template.groovy |
A custom file I created to add the graph template configuration from the ``docker/conf/janusgraph-template.properties` to the ConfiguredManagementGraph if it does not already exists. Without this template, you will not be able to create new graphs. |
janusgraph-configuredgraphfactory-demo/src/main/kotlin/Main.kt |
A Kotlin file showing an example of how to programmatically |
janusgraph-configuredgraphfactory-demo/build.gradle |
The dependencies list. Right now, all that is needed is org.apache.tinkerpop:gremlin-driver:3.5.4 (3.5.4 is needed until JanusGraph is compatible with newer versions of Tinkerpop 3) |
First, you need to add the following entries to your hosts file. If you don't you won't be able to run the Kotlin application unless you do so from a docker container inside the jce-network
network that is specified in the docker-compose.yaml
file:
127.0.0.1 jce-janusgraph # the main janusgraph server
127.0.0.1 jce-cassandra # the cassandra instance
Run docker compose up
from the docker
directory and wait until you see that the Gremlin server has started successfully.
Next, just run Main.kt
inside the Kotlin application. Feel free to change the graphName
and createANode
parameters in the JanusGraphConfiguredGraphFactoryDemo.run()
method.
You should see the application logs state that a new graph was created and a new node was added. To verify this from the Gremlin console, run the following commands:
gremlin.sh
:remote connect tinkerpop.server conf/remote.yaml session
-- note thesession
on the end here. It allows us to save session variables:remote console
to run all commands against the remote serverConfiguredGraphFactory.getGraphNames()
should display a list of available graphsgraph = ConfiguredGraphFactory.open("myNewGraph")
to get an instance of the graph that was createdg = graph.traversal()
to get a traversal to operate against the graphg.V()
should display the same ID that was output by the Kotlin application- Celebrate!