普通JSON到GraphSON格式

时间:2017-07-05 13:48:57

标签: javascript json gremlin gremlin-server graphson

我有两个问题:

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

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

  3. 谢谢! 阿德里安

    [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}]}}
    

1 个答案:

答案 0 :(得分:3)

  

在哪里我可以找到GraphSON文件的基本格式,保证由gremlin控制台成功加载?

此时有多个版本的GraphSON。您可以在Apache TinkerPop IO Documentation中获取引用。当你写作时,"由gremlin控制台成功加载"我假设你的意思是GraphSONReader所描述的方法here。因此,您在上面显示的格式是您可以使用的一种形式。尽管您可以在wrapAdjacencyList选项设置为true的情况下构建读取器/写入器,但它不是有效的JSON,它将生成有效的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的原因是因为需要为Hadoop和其他分布式处理引擎拆分GraphSON文件的标准格式。因此,它为每个顶点生成一行 - StarGraph格式。

  

如果我能够创建JSON,当我启动它时,我在哪里告诉服务器将其作为基本图加载?在配置文件或脚本中?

脚本可行。就像TinkerGraph上的gremlin.tinkergraph.graphLocationgremlin.tinkergraph.graphFormat configuration 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过于复杂。