使用Groovy使用Gremlin读取GraphML文件的正确方法

时间:2016-03-05 12:00:41

标签: groovy gremlin

我正在尝试读取GraphML文件并将其与Gremlin遍历一起使用。我使用的是与此页面相同的代码:

http://tinkerpop.apache.org/docs/3.1.1-incubating/reference/

我用writeGraph编写示例图并用readGraph读回来。图上的toString()调用就可以实现 看起来图形是正确读取的(每个都有6个节点和6个顶点),但是只应用Gremlin遍历 为TinkerFactory图生成输出,而不是读入的输出。

代码

@Grab('org.apache.tinkerpop:tinkergraph-gremlin:3.1.1-incubating')
@Grab('org.apache.tinkerpop:gremlin-core:3.1.1-incubating')

import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.io.IoCore;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;

final Graph graph = TinkerFactory.createModern();
graph.io(IoCore.graphml()).writeGraph("test.xml");
final Graph newGraph = TinkerGraph.open();
newGraph.io(IoCore.graphml()).readGraph("test.xml");

def g = graph.traversal();
def n = newGraph.traversal();

println("Graph")
println(g.toString())
g.V(1).out().values('name').sideEffect{println it}.iterate()

println("newGraph")
println(n.toString())
n.V(1).out().values('name').sideEffect{println it}.iterate()

println("DONE")

输出

Graph
graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
lop
vadas
josh
newGraph
graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
DONE

代码似乎与GraphSON和Gyro输出一起使用。

1 个答案:

答案 0 :(得分:2)

以下是Gremlin-Users邮件列表中的related post。当您在newGraph中阅读时,ID将被视为String。您可以通过配置vertexIdManager来改变此行为:

def conf = new org.apache.commons.configuration.BaseConfiguration();
conf.setProperty("gremlin.tinkergraph.vertexIdManager","LONG");
final Graph newGraph = TinkerGraph.open(conf);
newGraph.io(IoCore.graphml()).readGraph(dest);