2

I am trying to initialize an in-memory graph using TinkerGraph.

Firstly, i have defined the bean in my context xml file and tried to initialise the TinkerGraph.

My intention is to unit test the classes that i have created for forming the gremlin queries, the end queries that i get from these classes are in the form of a string, so in order to execute them through the TinkerGraph, i have the used the approach given in the following post: Get Gremlin query as a String and execute it in java without submitting it to the GremlinServer

I would also like to know whether the approach that i have taken is the preferred approach, for running the gremlin queries as part of the unit testing?

Following are the dependencies i have included in the pom.xml:

<dependency>
    <groupId>org.apache.tinkerpop</groupId>
    <artifactId>tinkergraph-gremlin</artifactId>
    <version>3.2.4</version>
</dependency>

<dependency>
      <groupId>org.apache.tinkerpop</groupId>
      <artifactId>gremlin-groovy</artifactId>
      <version>3.0.2-incubating</version>
</dependency>

EmbeddedGremlinQueryEngine is as follows:


import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.List;

public class UcsEmbeddedGremlinQueryEngine implements GremlinEngine{

    private static final Logger logger = LoggerFactory.getLogger(UcsEmbeddedGremlinQueryEngine.class);
    private GraphTraversalSource graphTraversalSource = null;
    private Graph graph = null;
    private ScriptEngine engine = null;
    private Bindings bindings = null;

    public UcsEmbeddedGremlinQueryEngine() {
        graph = TinkerGraph.open();
        graphTraversalSource = graph.traversal();
        engine = new GremlinGroovyScriptEngine();
        bindings = engine.createBindings();
        bindings.put("g", graphTraversalSource);
    }

    public void shutdown() throws Exception {
        if (graph != null){
            graph.close();
        }
        logger.info("TinkerGraph shutdown complete.");
    }

    @Override
    public List<Result> query(String query) {
        List<Result> res = null;
        try {
            ResultSet results = (ResultSet) engine.eval(query, bindings);
            res = results.all().join();

            for (Result r : res) {
                System.out.println("result: " + r + '\n');
            }

        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return res;
    }

    // This function reads the initScript and run them as gremlin queries.
    public synchronized void initialize() {
        logger.debug("Initializing embedded TinkerGraph. This will only take a few seconds....");
        //TODO include the execution of queries as part of initialisation
    }
}

Stack trace is as follows:

java.lang.NoClassDefFoundError: org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversalSource$GraphTraversalSourceStub
    at org.apache.tinkerpop.gremlin.groovy.loaders.StepLoader.load(StepLoader.groovy:54)
    at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:236)
    at org.apache.tinkerpop.gremlin.groovy.loaders.GremlinLoader.load(GremlinLoader.groovy:28)
    at org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.<init>(GremlinGroovyScriptEngine.java:189)
    at org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.<init>(GremlinGroovyScriptEngine.java:172)
    at com.intuit.gro.mcsdata.gemlinengine.UcsEmbeddedGremlinQueryEngine.<init>(UcsEmbeddedGremlinQueryEngine.java:28)

EmbeddedGremlinQueryEngine is defined as a bean in the xml file, when the bean is loaded i get the error as Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversalSource$GraphTraversalSourceStub

I don't understand how the GraphTraversalSourceStub comes into picture during initialization, I was not able to find any information about it. Any help would be appreciated.

  • well, the NCDFException is somewhat expected because there is no class in TinkerPop called `GraphTraversalSourceStub` that I know of, but I don't know what part of your code is trying to reference that. Perhaps you should post the entire stacktrace for the exception and maybe update your question to talk more about the libraries your using in conjunction with this code? I sense a DI framework in the picture? – stephen mallette Jun 10 '19 at 10:52
  • Hi @stephenmallette I have updated the question with more details as you pointed out, and also the intention of why i have written this class, if there is any better approach for it, then please let me know. – Deepak Nainani Jun 10 '19 at 13:19

2 Answers2

1

I think your problem is that you're:

  1. using really really old versions of TinkerPop
  2. the old versions you're using are probably incompatible

I'm not sure if you have a reason for using 3.2.4, but if so, make sure gremlin-groovy is also 3.2.4. Note that the 3.2.x line of code is largely not maintained at this point with the last release being 3.2.11 about 6 months ago. If you are developing a new application then I highly recommend that you simply utilize the latest version of 3.4.2 which released a few weeks ago.

As for your testing approach, I suppose that's fine. If you have test Gremlin strings then you really don't have much other choice in the matter, short of using Gremlin Server. Obviously providing a test harness for GremlinGroovyScriptEngine is a lot easier to do.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • i was actually trying to experiment using different versions, but i am getting the same error, I have also tried the latest version 3.4.2 which you mentioned. – Deepak Nainani Jun 10 '19 at 14:07
  • it's almost never a good idea to mix/match versions. you may hit subtle (or major) problems if you don't know how the versions work together. i mentioned "dependency injection" in my earlier comment and now i see your question tag for "spring". is Spring somehow creating that dynamically for injection purposes? like, what happens if you ignore spring and just create a simple Class with a `public static void main(String[] args)` method and just instantiate this class? does the class work without spring involved? – stephen mallette Jun 10 '19 at 17:33
0

For version 3.4.0 you need at least the following classpath.

set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-core\3.4.0\gremlin-core-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-driver\3.4.0\gremlin-driver-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-groovy\3.4.0\gremlin-groovy-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-server\3.4.0\gremlin-server-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-shaded\3.4.0\gremlin-shaded-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\tinkergraph-gremlin\3.4.0\tinkergraph-gremlin-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\tinkerpop\3.4.0\tinkerpop-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\commons-configuration\commons-configuration\1.10\commons-configuration-1.10.jar
set cp=%cp%;C:\pathToM2Repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\logging\log4j\log4j-slf4j-impl\2.11.1\log4j-slf4j-impl-2.11.1.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\logging\log4j\log4j-core\2.11.1\log4j-core-2.11.1.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy\2.5.4\groovy-2.5.4-indy.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy-json\2.5.4\groovy-json-2.5.4-indy.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy-xml\2.5.5\groovy-xml-2.5.5.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy-templates\2.5.5\groovy-templates-2.5.5.jar
set cp=%cp%;C:\pathToM2Repo\org\javatuples\javatuples\1.2\javatuples-1.2.jar
set cp=%cp%;C:\pathToM2Repo\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar
set cp=%cp%;C:\pathToM2Repo\io\netty\netty-all\4.1.31.Final\netty-all-4.1.31.Final.jar
Ujjwal Pathak
  • 646
  • 12
  • 21