Search code examples
janusgraph

How to create a Janusgraph instance within my program to access the custom graph


JanusGraph: I have created a custom graph using ConfiguredGraphFactory and able to access this graph using the gremlin console. How can I access this graph from my Scala code?

Currently I am running a remote gremlin server and connect to this remote server from my code to preform transactions on my custom graph. I am wondering whether there are any ways to create a janusgraph instance in my program and access the graph rather than through remote server.

Janus Graph Version: 0.2.0


Solution

  • Currently, in TinkerPop compliant systems, you cannot instantiate a local graph reference that acts upon a remote graph reference. However, you can use withRemote to instantiate a local graph traversal object backed by a remote graph traversal reference:

    gremlin> cluster = Cluster.open('conf/remote-objects.yaml')
    ==>localhost/127.0.0.1:8182
    gremlin> graph = EmptyGraph.instance()
    ==>emptygraph[empty]
    gremlin> g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "g"))
    ==>graphtraversalsource[emptygraph[empty], standard]
    gremlin> g.V().valueMap(true)
    ==>[name:[marko],id:1,label:person,age:[29]]
    ==>[name:[vadas],id:2,label:person,age:[27]]
    ==>[name:[lop],id:3,label:software,lang:[java]]
    ==>[name:[josh],id:4,label:person,age:[32]]
    ==>[name:[ripple],id:5,label:software,lang:[java]]
    ==>[name:[peter],id:6,label:person,age:[35]]
    ==>[name:[matthias],id:13,label:vertex]
    gremlin> g.close()
    gremlin> cluster.close()
    

    However, since you are using the ConfiguredGraphFactory, I will assume your graphs are created dynamically. This would mean that your graphs and traversal objects are not bound to any variable names on the remote servers since these bindings are traditionally formed during the instantiation of the graphs defined your gremlin-server.yaml graphs {} object. As you can see above, the only way to use withRemote is to supply the name of the variable bound to the graph traversal on the remote server. JanusGraph does not currently support dynamically updating the server's global bindings, but once it does, then you will be able to use the withRemote method to get a local reference to a remote traversal object. If you need to work with local graph objects bound to remote graph references, you would need to work with the TinkerPop community to enable such functionality. For more information on this matter, please see this TinkerPop Jira.

    Another solution is to remove the layer of your remote JanusGraph servers. If you are looking to run the graph processing locally, then perhaps you don't need remote JanusGraph servers at all. You could then instantiate graph references using the JanusGraphFactory and perform your queries on local graph references which talk directly to the backend datastores.

    Finally, assuming you configured your remote JanusGraph servers and the backend data stores (i.e. you know how the remote JanusGraph servers are configured to talk to the backend datastores -- this would be the configurations saved on your ConfigurationManagementGraph), you could leave your remote JanusGraph servers alone, and instantiate local graph references by using the JanusGraphFactory to open a properties file with the same configuration as that for your graph as defined by the ConfigurationManagementGraph.