我正在尝试使用 TinkerGraph 初始化内存中的图。
首先,我在上下文 xml 文件中定义了 bean,并尝试初始化 TinkerGraph。
我的目的是对我为形成 gremlin 查询而创建的类进行单元测试,我从这些类中获得的最终查询是字符串的形式,所以为了通过 TinkerGraph 执行它们,我使用了以下帖子中给出的方法: Get Gremlin query as a String and execute it in java without submit it to the GremlinServer
我还想知道我采用的方法是否是首选方法,用于将 gremlin 查询作为单元测试的一部分运行?
以下是我在 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 如下:
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
}
}
堆栈跟踪如下:
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 被定义为 xml 文件中的 bean,当加载 bean 时,由于构造函数抛出异常,我得到错误;嵌套异常是 java.lang.NoClassDefFoundError: org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversalSource$GraphTraversalSourceStub
我不明白 GraphTraversalSourceStub 在初始化过程中是如何出现的,我找不到任何有关它的信息。任何帮助,将不胜感激。