Programmatically add global variables to gremlin s

2019-08-02 13:37发布

问题:

How do I add global variables to an embedded Gremlin server instance?

Also, I want to avoid loading the server configuration from a file, although I can load resources from the classpath.

回答1:

getGlobalBindings() on GremlinExecutor is indeed deprecated, but the javadoc explains how you should proceed:

replaced by getScriptEngineManager() to add global scoped bindings directly to that object.

That comes from the 3.2.5 javadoc when it was originally deprecated in preparation for pretty large changes in 3.3.0 when new interfaces were implement to better generalize the GremlinScriptEngine. While these new interfaces were defined for default use in 3.3.0, they are actually present in 3.2.x and may be used there. Note that the getGlobalBindings() method was actually removed completely in 3.3.0 so when you upgrade you will end up with compilation errors.

Where there may be some confusion with respect to that javadoc comment is that to use the getScriptEngineManager() you must also use what is the default 3.3.0 yaml configuration on the 3.2.x line of code...an example is shown here:

https://github.com/apache/tinkerpop/blob/3.3.0/gremlin-server/conf/gremlin-server-classic.yaml#L25

Note that under this new model, you have two other options for adding global bindings...you could also either:

  1. Use the BindingsGremlinPlugin to add global bindings programmatically
  2. Write your own GremlinPlugin instance to add your bindings


回答2:

Looks like we can do it this way, although getGlobalBindings() is deprecated.

    Graph graph = this.createGraph();
    GraphTraversalSource g = graph.traversal();

    this.server = new GremlinServer(getSettings());
    this.server.getServerGremlinExecutor().getGraphManager().putGraph("graph", graph);
    this.server.getServerGremlinExecutor().getGremlinExecutor().getGlobalBindings().put("graph", graph);
    this.server.getServerGremlinExecutor().getGremlinExecutor().getGlobalBindings().put("g", g);

    this.server.start();