我正在尝试使用TinkerGraph初始化内存中的图形。
首先,我在上下文XML文件中定义了bean,并尝试初始化TinkerGraph。
我的目的是对我创建的用于形成gremlin查询的类进行单元测试,我从这些类中获得的最终查询采用字符串形式,因此为了通过TinkerGraph执行它们,我必须使用了以下文章中给出的方法: Get Gremlin query as a String and execute it in java without submitting 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在初始化期间是如何出现的,我无法找到有关它的任何信息。 任何帮助将不胜感激。
答案 0 :(得分:0)
我认为您的问题是您是
我不确定您是否有使用3.2.4的理由,但是如果是这样,请确保gremlin-groovy也是3.2.4。请注意,目前基本上未维护3.2.x代码行,最新版本为6个月前的3.2.11。如果您要开发新的应用程序,那么我强烈建议您简单地使用几周前发布的最新版本3.4.2。
至于您的测试方法,我想很好。如果您已经测试了Gremlin字符串,那么除了使用Gremlin Server之外,您实际上没有其他选择。显然,为GremlinGroovyScriptEngine
提供测试工具要容易得多。
答案 1 :(得分:0)
对于3.4.0版,您至少需要以下类路径。
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