Skip to content

Instantly share code, notes, and snippets.

@baughmann
Created December 8, 2022 21:34
Show Gist options
  • Save baughmann/3336984f2f0f43fa6d20f4b9b821eac1 to your computer and use it in GitHub Desktop.
Save baughmann/3336984f2f0f43fa6d20f4b9b821eac1 to your computer and use it in GitHub Desktop.
JanusGraph Management in Kotllin (Java)
// Portions of this example assume that:
// 1. You are using [ConfiguredGraphFactory](https://docs.janusgraph.org/operations/configured-graph-factory/) to manage multiple graphs in JanusGraph
// 2. For the above, that you have already added a default graph template using `ConfiguredGraphFactory.createTemplateConfiguration()`.
// connect to a JanusGraph instance running on localhost
val cluster = Cluster.build("localhost").create()
val session = cluster.connect<Client.SessionedClient>()
// get a list of graph names (List<Result> which needs to be acted upon differently than a List<String>)
val graphNames = client.submit("ConfiguredGraphFactory.getGraphNames()").all().get()
// create a graph called "MyNewGraph" if it does not already exist
val myGraphName = "MyNewGraph"
if(!graphNames.any { it.string == myGraphName }) {
session.submit("ConfiguredGraphFactory.create('${myGraphName}')")
}
// get a reference to the graph
session.submit("graph = ConfiguredGraphFactory.open('${myGraphName}')")
// get a reference to the management system
session.submit("mgmt = graph.openManagement()")
// get or create a property
val nameResult = session.submit("name = mgmt.getPropertyKey('name')").all().get().first()
if (nameResult.isNull) {
session.submit("name = mgmt.makePropertyKey('name').dataType(String.class).make()")
// commit
session.submit("mgmt.commit()")
// re-open the management system
session.submit("mgmt = graph.openManagement()")
// get the property
session.submit("name = mgmt.getPropertyKey('name')")
}
// get or create an index on the property
val result = session.submit("mgmt.getGraphIndex('name')").all().get().first()
if (result.isNull) {
session.submit("mgmt.buildIndex('name', Vertex::class.java).addKey(mgmt.getPropertyKey('name')).buildCompositeIndex()")
} else {
session.submit("i = mgmt.getGraphIndex('name')")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment