Search code examples
javaorientdbgremlin

How to retrieve all graph names from the gremlin server


My gremlin-server.yaml file is as follows:

host: localhost
port: 8182
scriptEvaluationTimeout: 30000
channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer
graphManager : com.orientechnologies.tinkerpop.server.OrientGremlinGraphManager
graphs: {
  graph : ../config/db1.properties,
  graph2 : ../config/db2.properties
}
scriptEngines: {
  gremlin-groovy: {
    plugins: { org.apache.tinkerpop.gremlin.server.jsr223.GremlinServerGremlinPlugin: {},
               org.apache.tinkerpop.gremlin.orientdb.jsr223.OrientDBGremlinPlugin: {},
               org.apache.tinkerpop.gremlin.jsr223.ImportGremlinPlugin: {classImports: [java.lang.Math], methodImports: [java.lang.Math#*]},
               org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {files: [../config/db.groovy]}}}}
serializers:
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { ioRegistries: [org.apache.tinkerpop.gremlin.orientdb.io.OrientIoRegistry] }}             # application/vnd.gremlin-v3.0+gryo
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}                                                                       # application/vnd.gremlin-v3.0+gryo-stringd
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0, config: { ioRegistries: [org.apache.tinkerpop.gremlin.orientdb.io.OrientIoRegistry] }}         # application/json
processors:
  - { className: org.apache.tinkerpop.gremlin.server.op.session.SessionOpProcessor, config: { sessionTimeout: 28800000 }}
  - { className: org.apache.tinkerpop.gremlin.server.op.traversal.TraversalOpProcessor, config: { cacheExpirationTime: 600000, cacheMaxSize: 1000 }}

I am using java to connect to the gremlin server. Is there a way to retrieve the graph names: graph and graph2 from code?

Alternatively if I bind graph and graph2 traversals to g and g2 in the db.groovy file and add them as global bindings, is there a way to retrieve the names: g and g2?


Solution

  • There is no direct API to get this listing but there is a workaround to get it if you use a script-based request and your provider implementation has not disallowed this for security reason (or simply doesn't support it given the way it has implemented the protocol). In short, I would only expect this approach to work with TinkerPop's Gremlin Server implementation and if security sandboxing is disabled or configured to allow access to the classes involved.

    Gremlin Server hosts a ScriptEngine instance that process scripts. It has a "context" which is available as a variable with the same name. You can access that variable with:

    gremlin> :remote connect tinkerpop.server conf/remote.yaml
    ==>Configured localhost/127.0.0.1:8182
    gremlin> :remote console
    ==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182] - type ':remote console' to return to local mode
    gremlin> context
    ==>org.apache.tinkerpop.gremlin.jsr223.GremlinScriptContext@c7ef4c5
    

    Once you have that, you can filter out Graph (or more likely you would fitler GraphTraversalSource instances) and get the name it is known by on the server:

    gremlin> context.getBindings(javax.script.ScriptContext.GLOBAL_SCOPE).entrySet().findAll{it.value instanceof Graph}.key
    ==>graph
    gremlin> context.getBindings(javax.script.ScriptContext.GLOBAL_SCOPE).entrySet().findAll{it.value instanceof GraphTraversalSource}.key
    ==>g
    

    As you can see it works just as well with a compliant driver:

    gremlin> cluster = Cluster.open()
    ==>localhost/127.0.0.1:8182
    gremlin> client = cluster.connect()
    ==>org.apache.tinkerpop.gremlin.driver.Client$ClusteredClient@5408d4b3
    gremlin> client.submit("context.getBindings(javax.script.ScriptContext.GLOBAL_SCOPE).entrySet().findAll{it.value instanceof GraphTraversalSource}.key").all().get()
    ==>result{object=g class=java.lang.String}