简体   繁体   中英

Use custom lib in gremlin server script file

I have two question please, if that's alright?

  1. I was wondering how to call (use) my own custom written java code in gremlin server script engine please? so, for example, I think Stephen Mallette wrote some java functions, which are called inside of gremlin server script folder, here is a link to his functions:

https://github.com/apache/tinkerpop/blob/master/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerFactory.java

And then in Gremlin Server script folder, generateModern(final TinkerGraph g) was use as below:

globals << [hook : [
  onStartUp: { ctx ->
    ctx.logger.info("Loading 'modern' graph data.")
      org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory.generateModern(graph)
  }
] as LifeCycleHook]

How to I replace this with my own written java functions please, in a way that gremlin server will understand my java functions and read them at runtime? Do I need to compile my functions into a Jar file and put in the Lib folder?

  1. I am connecting to Gremlin Server via a Java program i built and not Gremlin console. How do i reference and use the graph object variable in my java program. I can only see and use the graph traversal source object variable (g), which is expose in the script file ie
globals << [g: graph.traversal()]

How do I expose the graph object variable ie graph so that i can use it in my Java program?

Many thanks

If you have your own Java library that you wish to expose in Gremlin Server just package it into a jar and add it to the Gremlin Server classpath. You can do this manually by just copying the jar to the /lib directory or add it via gremlin-server.sh install command. If you go with the latter you may consider building GremlinPlugin class (it's an interface you implement) which will allow you to set up class imports to the Gremlin Server ScriptEngine. You can see an example of such a simple plugin here . With that jar file in place you can then access your library's classes in your scripts.

How do i reference and use the graph object variable in my java program

You can only access "graph" in a script from your Java program. It is not possible to do that with bytecode requests. That said, you should not use the Graph object at all really except to spawn "g". Other uses are not recommended as the best practice is to only use Gremlin to access your graph and you can only write Gremlin with "g". Using "graph" will definitely limit code portability and bind you to APIs that might change or not be wholly supported by all graphs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM