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

Correct Way to Read GraphML files with Gremlin using Groovy

我正在尝试读取 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 和陀螺仪输出。

这是来自 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);