[英]Serialize DefaultGraphTraversal (from gremlin query) to GraphSON v3 json output
I'm using GremlinGroovyScriptEngine to eval a gremlin query (POSTed from REST api). 我正在使用GremlinGroovyScriptEngine评估gremlin查询(从REST API发布)。 The result of which returns a DefaultGraphTraversal object.
其结果返回DefaultGraphTraversal对象。 I am trying to serialize this into a structure similar to
我正在尝试将其序列化为类似于
"result": {
"data": {
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "Identity~1234567",
"label": "Identity",
"properties": {
"object_identifier": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": -710449208
},
"value": "1234567",
"label": "object_identifier"
}
}
]
}
}
},
.... more results here
I have tried using ObjectMapper like this 我已经试过像这样使用ObjectMapper
mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper.version(GraphSONVersion.V3_0).create.createMapper
and this ... 和这个 ...
GraphSONMapper.build().
addRegistry(com.lambdazen.bitsy.BitsyIoRegistryV3d0.instance()).
version(GraphSONVersion.V3_0).create().createMapper()
and other variations of the above. 以及上述内容的其他变体。 Howerver, it gets deserialized to something like
但是,它反序列化为类似
{"@type":"g:List","@value":[]}
but the individual items of the list don't get serialized correctly. 但是列表中的各个项目未正确序列化。
Edit 编辑
Code example: gremlinQuery eg. 代码示例:gremlinQuery例如。 gV('id_12345')
gV('id_12345')
List<Object> results = ((DefaultGraphTraversal<Vertex, Object>) this.engineWrite.eval(gremlinQuery, this.bindingsWrite)).toList();
ObjectMapper mapper = writeGraph.io(GraphSONIo.build(GraphSONVersion.V3_0))
.mapper()
.version(GraphSONVersion.V3_0)
.create()
.createMapper();
mapper.writeValueAsString(results);
which results in 导致
{"@type":"g:List","@value":[]}
I have sort of got round this by iterating over the results and serializing thus: 我通过遍历结果并进行序列化来解决这个问题:
List<Object> resList = new ArrayList<>();
results.stream().forEach(list -> resList.add(list));
String data = mapper.writeValueAsString(resList);
which does yield the correct results, but just seems like I'm missing something vital in order to be able to do it in one step. 确实可以产生正确的结果,但是似乎我缺少一些重要的东西,以便能够一步一步完成。
What am I doing wrong here?? 我在这里做错了什么? Many thanks
非常感谢
I'm not able to recreate the problem (along the 3.4.x line of code): 我无法重新创建问题(沿着3.4.x代码行):
gremlin> bindings = new javax.script.SimpleBindings()
gremlin> bindings.put('g', TinkerFactory.createModern().traversal())
gremlin> engine = new org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine()
==>org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine@7b676112
gremlin> results = engine.eval("g.V(1)", bindings).toList()
==>v[1]
gremlin> mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@7f5b9db
gremlin> mapper.writeValueAsString(results)
==>{"@type":"g:List","@value":[{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":1},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":0},"value":"marko","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":1},"value":{"@type":"g:Int32","@value":29},"label":"age"}}]}}}]}
The empty result would only be expected if the "result" itself was an empty list, but you seem to indicate that this is not the case given that iteration of the result and serialization of its contents seems to work just fine. 仅当“结果”本身是一个空列表时,才可以预期为空结果,但是您似乎指示情况并非如此,因为结果的迭代和其内容的序列化似乎可以正常工作。 I would suggest a few options to try to debug:
我建议尝试调试的一些方法:
Map
using valueMap(true)
and see what happens there. valueMap(true)
以Map
返回数据,然后查看发生了什么。 If it works then perhaps there is something wrong with the "BitsyIoRegistry"? valueMap(true)
without "BitsyIoRegistry" included. valueMap(true)
。 If you can recreate the problem with TinkerGraph, then it's likely a problem with TinkerPop and will need to be addressed there. 如果您可以使用TinkerGraph重新创建问题,那么TinkerPop可能是有问题,需要在那里解决。 If it works for TinkerGraph then I assume it must be a problem with Bitsy somehow.
如果它适用于TinkerGraph,那么我认为Bitsy一定是有问题的。 Odd issue....
奇怪的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.