将普通的JSON转换为GraphSON格式

5
我有两个问题:
  1. 我在哪里可以找到保证能被Gremlin控制台成功加载的GraphSON文件的基本格式?我正在尝试将一个具有10-20个字段的JSON转换为另一个可由Gremlin查询的文件,但实际上我找不到有关图形格式保留字段或如何处理ID等信息的相关信息。我导出了他们提供的现代图表,但它甚至不是一个有效的JSON(多个JSON根元素),而是一个JSON列表[1]。我还看到像outE、inE这样的字段...这些字段是我手动创建的吗?

  2. 如果我能创建JSON,我在哪里告诉服务器在启动时将其作为基础图形加载?在配置文件中还是脚本中?

谢谢! Adrian

[1] https://pastebin.com/drwXhg5k

{"id":1,"label":"person","outE":{"created":[{"id":9,"inV":3,"properties":{"weight":0.4}}],"knows":[{"id":7,"inV":2,"properties":{"weight":0.5}},{"id":8,"inV":4,"properties":{"weight":1.0}}]},"properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":1,"value":29}]}}
{"id":2,"label":"person","inE":{"knows":[{"id":7,"outV":1,"properties":{"weight":0.5}}]},"properties":{"name":[{"id":2,"value":"vadas"}],"age":[{"id":3,"value":27}]}}
{"id":3,"label":"software","inE":{"created":[{"id":9,"outV":1,"properties":{"weight":0.4}},{"id":11,"outV":4,"properties":{"weight":0.4}},{"id":12,"outV":6,"properties":{"weight":0.2}}]},"properties":{"name":[{"id":4,"value":"lop"}],"lang":[{"id":5,"value":"java"}]}}
{"id":4,"label":"person","inE":{"knows":[{"id":8,"outV":1,"properties":{"weight":1.0}}]},"outE":{"created":[{"id":10,"inV":5,"properties":{"weight":1.0}},{"id":11,"inV":3,"properties":{"weight":0.4}}]},"properties":{"name":[{"id":6,"value":"josh"}],"age":[{"id":7,"value":32}]}}
{"id":5,"label":"software","inE":{"created":[{"id":10,"outV":4,"properties":{"weight":1.0}}]},"properties":{"name":[{"id":8,"value":"ripple"}],"lang":[{"id":9,"value":"java"}]}}
{"id":6,"label":"person","outE":{"created":[{"id":12,"inV":3,"properties":{"weight":0.2}}]},"properties":{"name":[{"id":10,"value":"peter"}],"age":[{"id":11,"value":35}]}}

请提供您想要转换的原始JSON文件,即使晚些时候我可能会有不同的方法可用。 - Wolfgang Fahl
1个回答

5
在哪里可以找到保证能够被Gremlin控制台成功加载的GraphSON文件的基本格式?目前有多个版本的GraphSON。您可以在Apache TinkerPop的IO文档IO Documentation中找到参考资料。当您写道“成功加载到Gremlin控制台”时,我假设您是指使用此处描述的GraphSONReader方法。如果是这样的话,那么您展示的格式是一种可用的形式。正如您所看到的,它不是有效的JSON格式,但您可以使用设置为truewrapAdjacencyList选项构建读取器/编写器,并且它将生成有效的JSON。以下是一个例子:
gremlin> graph = TinkerFactory.createModern();
==>tinkergraph[vertices:6 edges:6]
gremlin> writer =  graph.io(IoCore.graphson()).writer().wrapAdjacencyList(true).create()
==>org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter@24a298a6
gremlin> os = new FileOutputStream('wrapped-adjacency-list.json')
==>java.io.FileOutputStream@6d3c232f
gremlin> writer.writeGraph(os, graph)
gremlin> os.close()
gremlin> newGraph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> ins = new FileInputStream('wrapped-adjacency-list.json')
==>java.io.FileInputStream@7435a578
gremlin> reader = graph.io(IoCore.graphson()).reader().unwrapAdjacencyList(true).create()
==>org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader@63da207f
gremlin> reader.readGraph(ins, newGraph)
gremlin> newGraph
==>tinkergraph[vertices:6 edges:6]

默认情况下不会得到有效的JSON,这是因为GraphSON文件的标准格式需要支持Hadoop和其他分布式处理引擎的可拆分性。因此,它会产生每个顶点的一行-一个StarGraph格式。
如果我能创建JSON,那么我在哪里告诉服务器在启动时将其作为基础图形加载?在配置文件中还是脚本中?
脚本可以工作。在TinkerGraph上,gremlin.tinkergraph.graphLocationgremlin.tinkergraph.graphFormatconfiguration options也可以使用。
但总的来说,如果你已经有JSON并且没有加载数百万个图元素,最简单的方法可能就是解析它并使用标准的g.addV()g.addE()方法构建图形。
gremlin> import groovy.json.*
==>org.apache.tinkerpop.gremlin.structure.*,...
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> jsonSlurper = new JsonSlurper()
==>groovy.json.JsonSlurper@53e3a87a
gremlin> object = jsonSlurper.parseText('[{ "name": "John Doe" }, { "name" : "Jane Doe" }]')
==>[name:John Doe]
==>[name:Jane Doe]
gremlin> object.each {g.addV('name',it.name).iterate() }
==>[name:John Doe]
==>[name:Jane Doe]
gremlin> g.V().valueMap()
==>[name:[John Doe]]
==>[name:[Jane Doe]]

尝试将其转换为GraphSON相对于上述方法来说过于复杂。

我应该使用什么工具来轻松地将普通的JSON转换为GraphJSON,以便稍后加载和使用?(我使用TinkerPop) 顺便问一下,“wrapAdjacencyList”选项是什么,我在哪里设置它?我现在正在控制台中工作,稍后我会用JavaScript工作。 - Adrian Pop
请参阅Javadocs中的构建器:http://tinkerpop.apache.org/javadocs/current/core/org/apache/tinkerpop/gremlin/structure/io/graphson/package-summary.html,以获取`wrapAdjacencyList`选项。不确定是否回答了您所有的问题-我不知道您所说的“如何轻松将普通JSON转换为GraphJSON”的意思。 - stephen mallette
但是你如何在控制台中实际使用wrapAdjacencyList呢?我尝试了使用“graph.io(IoCore.graphson())。wrapAdjacencyList(“true”)。writeGraph(“tinkerpop-modern.json”)”,但是我收到以下错误:org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONIo.wrapAdjacencyList()方法没有签名适用于参数类型:(java.lang.String)值:[true]。 - Adrian Pop
顺便问一下,如果我用第一种方法成功创建了一个图表,如果我在配置文件中使用gremlin.tinkergraph.graphLocation和gremlin.tinkergraph.graphFormat,那么我就可以立即使用它了,对吗?graphLocation属性将是指向我刚生成的JSON文件的路径,并且它将自动加载和解析它。 - Adrian Pop
"最简单的方法可能是解析它,然后使用标准的g.addV()和g.addE()方法来构建图形。是的,但我实际上必须从JavaScript中执行此操作,我将无法访问控制台。我将拥有一个可查询的数据库,并返回一个JSON。我希望根据该JSON按需创建图形(每次都会生成另一个gremlin服务器)。实际上,我想要使用以前下载并转换为GraphSON格式的JSON构建现有图形的服务器。 使用JavaScript...我真的无法以另一种方式实现此目的。" - Adrian Pop
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接