我正在使用GremlinGroovyScriptEngine评估一个gremlin查询(从REST API发布)。其结果返回DefaultGraphTraversal对象。我正在尝试将其序列化为类似于
的结构"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
我曾尝试使用ObjectMapper这样
mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper.version(GraphSONVersion.V3_0).create.createMapper
还有这个...
GraphSONMapper.build().
addRegistry(com.lambdazen.bitsy.BitsyIoRegistryV3d0.instance()).
version(GraphSONVersion.V3_0).create().createMapper()
以及上述内容的其他变体。 但是,它反序列化为类似
{"@type":"g:List","@value":[]}
但是列表中的各个项目未正确序列化。
修改
代码示例: gremlinQuery例如。 g.V('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);
这将导致
{"@type":"g:List","@value":[]}
我可以通过遍历结果并进行序列化来解决这个问题:
List<Object> resList = new ArrayList<>();
results.stream().forEach(list -> resList.add(list));
String data = mapper.writeValueAsString(resList);
确实可以产生正确的结果,但似乎我缺少一些重要的东西,以便能够一步一步完成。
我在这里做错了什么? 非常感谢
答案 0 :(得分:0)
我无法重新创建问题(沿着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"}}]}}}]}
仅当“结果”本身是一个空列表时,才可以预期为空结果,但是您似乎表明情况并非如此,因为结果的迭代和其内容的序列化似乎可以正常工作。我建议您尝试一些调试选项:
Map
以valueMap(true)
的形式返回数据,然后查看发生了什么。如果可行,那么“ BitsyIoRegistry”可能有问题吗?valueMap(true)
,我都会尝试。如果您可以使用TinkerGraph重新创建问题,则TinkerPop可能存在问题,需要在那里解决。如果它适用于TinkerGraph,那么我认为Bitsy一定是有问题的。奇怪的问题。...