普通JSON到GraphSON格式 [英] Normal JSON to GraphSON format

查看:114
本文介绍了普通JSON到GraphSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题:


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

  1. Where I can actually find the basic format for a GraphSON file, that is guaranteed to be successfully loaded by the gremlin console? I'm trying to convert a JSON (with about 10-20 fields) to another file that can be queried by gremlin but I can't actually find any relevant information about the fields reserved by the graphson format or how I should handle the IDs etc. I exported the modern graph they provide and it's not even a valid JSON (Multiple JSON root elements), but a list of JSONs [1] I also saw fields like outE, inE...are these fields something I manually have to create?

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

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?

谢谢!
Adrian

Thanks! 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}]}}


推荐答案


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

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文档中获取参考。当您编写由gremlin控制台成功加载时,我认为您的意思是使用<$ h $ =http://tinkerpop.apache.org/上的 GraphSONReader 方法docs / current / reference /#graphson-reader-writerrel =nofollow noreferrer>这里。因此,您在上面显示的格式是您可以使用的一种形式。尽管你可以使用 wrapAdjacencyList 选项设置为 true 它将生成有效的JSON。以下是一个示例:

There are multiple versions of GraphSON at this point. You can get a reference in the Apache TinkerPop IO Documentation. When you write, "successfully loaded by the gremlin console" I assume that you mean with the GraphSONReader methods described here. Of so, then the format you show above is one form you can use. It is not valid JSON as you can see, though you can build the reader/writer with the wrapAdjacencyList option set to true and it will produce valid JSON. Here is an example:

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 格式。

The reason you do not get valid JSON by default is because the standard format for a GraphSON file needs to be splittable for Hadoop and other distributed processing engines. Therefore it produces one line per vertex - a StarGraph format.


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

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.graphLocation gremlin.tinkergraph.graphFormat 配置选项

最终,如果你有现有的JSON,你没有加载数以千万计的图元素,最简单的解析它并使用标准的 g.addV() g.addE()构建图表的方法:

Ultimately though, if you have existing JSON and you aren't loading tens of millions of graph elements, it is probably easiest to just parse it and use standard g.addV() and g.addE() methods to build the graph:

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过于复杂。

Trying to convert that to GraphSON is overly complicated compared to the approach above.

这篇关于普通JSON到GraphSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆