2017-07-05 44 views
2

我有兩個問題:正常JSON到GraphSON格式

  1. 我在哪裏可以找到真正的GraphSON文件的基本格式,即保證被小鬼控制檯加載成功?我試圖將一個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}]}} 

回答

3

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。如此,那麼上面顯示的格式就是您可以使用的一種格式。雖然可以使用wrapAdjacencyList選項設置爲true來構建讀寫器,但它會生成有效的JSON,因此它不是有效的JSON。這裏有一個例子:

gremlin> graph = TinkerFactory.createModern(); 
==>tinkergraph[vertices:6 edges:6] 
gremlin> writer = graph.io(IoCore.graphson()).writer().wrapAdjacencyList(true).create() 
==>or[email protected]24a298a6 
gremlin> os = new FileOutputStream('wrapped-adjacency-list.json') 
==>[email protected] 
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') 
==>[email protected] 
gremlin> reader = graph.io(IoCore.graphson()).reader().unwrapAdjacencyList(true).create() 
==>or[email protected]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?

腳本會起作用。 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() 
==>[email protected] 
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與上述方法相比過於複雜。

+0

我應該如何輕鬆地將普通JSON轉換爲可以稍後加載和使用的GraphJSON? (我使用TinkerPop,是的)。 順便說一句,什麼是「wrapAdjacencyList」選項,我在哪裏設置它?我現在在控制檯中工作,稍後我將使用JavaScript。 – Adi

+0

查看javadoc中的構建器:http://tinkerpop.apache.org/javadocs/current/core/org/apache/tinkerpop/gremlin/structure/io/graphson/package-summary.html用於'wrapAdjacencyList'選項。不知道這是否回答你所有的問題 - 我不知道你的意思是「輕鬆地將普通的JSON轉換爲GraphJSON」。 –

+0

但是,您如何在控制檯中實際使用wrapAdjacencyList?我嘗試了 「graph.io(IoCore.graphson())。wrapAdjacencyList(」true「)。writeGraph(「tinkerpop-modern.json」)「但我收到以下錯誤:沒有方法的簽名:org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONIo.wrapAdjacencyList()適用於參數類型:(java .lang.String)values:[true] – Adi