javascript - 普通 JSON 到 GraphSON 格式

标签 javascript json gremlin gremlin-server graphson

我有两个问题:

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

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

谢谢! 阿德里安

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

最佳答案

Where I can actually find the basic format for a GraphSON file, that is guaranteed to be successfully loaded by the gremlin console?

此时有多个版本的 GraphSON。您可以在 Apache TinkerPop IO Documentation 中获得引用.当您写下“由 gremlin 控制台成功加载”时,我假设您的意思是使用描述的 GraphSONReader 方法 here .当然,您上面显示的格式是您可以使用的一种形式。如您所见,它不是有效的 JSON,尽管您可以将 wrapAdjacencyList 选项设置为 true 来构建读取器/写入器,它将生成有效的 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 格式。

If I am able to create the JSON, where do I tell the server to load it as the base graph when I start it? In the config file or in the script?

一个脚本就可以了。与 gremlin.tinkergraph.graphLocationgremlin.tinkergraph.graphFormat configuration options 一样在 TinkerGraph 上。

最终,如果您有现有的 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 过于复杂。

关于javascript - 普通 JSON 到 GraphSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928121/

相关文章:

javascript - 如果我想在 .then() 触发后做某事,可以在 JavaScript 中使用 Promise

javascript - Facebook 连接使用 IE8 导致问题;任何解决方法?

java - 设置 Titan 和 Cassandra - 无法实例化存储管理器类

javascript - JSON google geocode API 获取国家

java - org.codehaus.jackson.JsonParseException : Unexpected character ('/' (code 47))

java - 按边标签对顶点组的传入顶点进行分组

java - WebSocketGremlinRequestEncoder 必须生成至少一条消息 - 使用 withRemote "sideEffect"的 janusgraph-dynamodb 不起作用

javascript - 使用 Jquery 用索引或位置而不是键名解析 JSON 对象?

javascript - 在类中,在另一个方法中调用 addEventlistener 的方法中的 HTML 元素

javascript - 向 JSON 字符串添加新属性