简体   繁体   English

将DefaultGraphTraversal(来自gremlin查询)序列化为GraphSON v3 json输出

[英]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: 我建议尝试调试的一些方法:

  1. Try to return the data as a Map using valueMap(true) and see what happens there. 尝试使用valueMap(true)Map返回数据,然后查看发生了什么。 If it works then perhaps there is something wrong with the "BitsyIoRegistry"? 如果可以,那么“ BitsyIoRegistry”可能有问题?
  2. I'd try both with and without valueMap(true) without "BitsyIoRegistry" included. 我将尝试在不包含“ BitsyIoRegistry”的情况下使用和不使用valueMap(true)
  3. Try all of this with TinkerGraph. 使用TinkerGraph尝试所有这些。

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.

相关问题 JanusGraph将子图输出为GraphSON错误 - JanusGraph output subgraph as GraphSON error 如何使用Jackson将自定义方法的输出序列化为JSON? - How to serialize output from custom method to JSON using Jackson? 使用Java客户端库从Google Calendar API v3查询事件? - Query events from Google Calendar API v3 using Java client library? 如何使用gremlin查询将json保存为顶点属性值 - How to save json as vertex properties values using gremlin query 通过 Spark 在 Janusgraph 上进行 Gremlin 查询。 错误:提供商 org.janusgraph.hadoop.serialize.JanusGraphKryoShimService 无法实例化 - Gremlin query on Janusgraph through Spark. Error: Provider org.janusgraph.hadoop.serialize.JanusGraphKryoShimService could not be instantiated 使用来自 .net 和 java 的 jUddi v3 - Using jUddi v3 from .net and java OrientDB将查询结果直接序列化到Json - OrientDB serialize a query result directly to Json 怎么把Antlr v2转换成v3语法? - How to convert from antlr v2 to v3 grammar? Searchanalytics查询JAVA API v3(Google WebMaster工具) - Searchanalytics Query JAVA API v3 (Google WebMaster Tools) 使用hashMap,YAML序列化时缺少输出 - missing output from serialize with hashMap, YAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM