在gremlin中的特定节点下以分层方式获取完整的图形信息(属性,id等)

时间:2017-06-06 14:26:38

标签: gremlin tinkerpop3

I am able to get all the child nodes and its related information under a parent node but in a array of objects format , for the query 

g.V(4128).repeat(out()).emit()


[
  {
    "id": 4152.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "1l3-37c-1l1",
          "value": "XYZ"
        }
      ],
      "description": [
        {
          "id": "1zb-37c-7wl",
          "value": "XYZ World"
        }
      ],
      "shortName": [
        {
          "id": "16v-37c-745",
          "value": "XYZ"
        }
      ]
    }
  },
  {
    "id": 4176.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "1l6-380-1l1",
          "value": "XYZ-XYZW"
        }
      ],
      "description": [
        {
          "id": "1ze-380-7wl",
          "value": "XYZ West Campus"
        }
      ],
      "shortName": [
        {
          "id": "16y-380-745",
          "value": "XYZW"
        }
      ]
    }
  },
  {
    "id": 8344.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "3kj-6fs-1l1",
          "value": "XYZ-XYZE"
        }
      ],
      "description": [
        {
          "id": "3yr-6fs-7wl",
          "value": "XYZ East Campus"
        }
      ],
      "shortName": [
        {
          "id": "36b-6fs-745",
          "value": "XYZE"
        }
      ]
    }
  },
  {
    "id": 4104.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "1kx-360-1l1",
          "value": "XYZ-XYZW-P1"
        }
      ],
      "description": [
        {
          "id": "1z5-360-7wl",
          "value": "XYZ West Campus-Phase-1"
        }
      ],
      "shortName": [
        {
          "id": "16p-360-745",
          "value": "P1"
        }
      ]
    }
  },
  {
    "id": 4296.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "1ll-3bc-1l1",
          "value": "XYZ-XYZW-P3"
        }
      ],
      "description": [
        {
          "id": "1zt-3bc-7wl",
          "value": "XYZ West Campus-Phase-3"
        }
      ],
      "shortName": [
        {
          "id": "17d-3bc-745",
          "value": "P3"
        }
      ]
    }
  },
  {
    "id": 8200.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "3k1-6bs-1l1",
          "value": "XYZ-XYZW-P2"
        }
      ],
      "description": [
        {
          "id": "3y9-6bs-7wl",
          "value": "XYZ West Campus-Phase-2"
        }
      ],
      "shortName": [
        {
          "id": "35t-6bs-745",
          "value": "P2"
        }
      ]
    }
  },
  {
    "id": 8224.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "3yc-6cg-1l1",
          "value": "XYZ-XYZE-P1"
        }
      ],
      "description": [
        {
          "id": "4ck-6cg-7wl",
          "value": "XYZ East Campus-Phase-1"
        }
      ],
      "shortName": [
        {
          "id": "3k4-6cg-745",
          "value": "P1"
        }
      ]
    }
  },
  {
    "id": 8392.0,
    "label": "location",
    "type": "vertex",
    "properties": {
      "displayName": [
        {
          "id": "3kp-6h4-1l1",
          "value": "XYZ-XYZE-P2"
        }
      ],
      "description": [
        {
          "id": "3yx-6h4-7wl",
          "value": "XYZ East Campus-Phase-2"
        }
      ],
      "shortName": [
        {
          "id": "36h-6h4-745",
          "value": "P2"
        }
      ]
    }
  }
]

我需要以层次结构的方式获得相同的信息。

我尝试了以下查询

g.V(4128).repeat(下())。发射()。树()。由(__。valueMap(真))

但我没有得到有效的json。

我需要以分层方式获取上述数据

更新
 我尝试了以下查询
    g.V(4128).repeat(out())。emit()。tree()。​​by(__。valueMap(true))

我收到的回复是

[
   {
"{label = org, name = [XYZ], orgId = [00000000-0000-0000-0000-000000000001], desc = [XYZ Organization], id = 4128}": {
  "{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 8272}": {

  },
  "{label = location, displayName = [XYZ], description = [XYZ World], shortName = [XYZ], id = 4152}": {
    "{label = location, displayName = [XYZ-XYZW], description = [XYZ West Campus], shortName = [XYZW], id = 4176}": {
      "{label = location, displayName = [XYZ-XYZW-P3], description = [XYZ West Campus-Phase-3], shortName = [P3], id = 4296}": {

      },
      "{label = location, displayName = [XYZ-XYZW-P2], description = [XYZ West Campus-Phase-2], shortName = [P2], id = 8200}": {

      },
      "{label = location, displayName = [XYZ-XYZW-P1], description = [XYZ West Campus-Phase-1], shortName = [P1], id = 4104}": {

      }
    },
    "{label = location, displayName = [XYZ-XYZE], description = [XYZ East Campus], shortName = [XYZE], id = 8344}": {
      "{label = location, displayName = [XYZ-XYZE-P1], description = [XYZ East Campus-Phase-1], shortName = [P1], id = 8224}": {

      },
      "{label = location, displayName = [XYZ-XYZE-P2], description = [XYZ East Campus-Phase-2], shortName = [P2], id = 8392}": {

      }
    }
  },
  "{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 8248}": {

  },
  "{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 12488}": {

  }
}

} ]

现在您可以看到所收到的json不合适,因为该键包含'='分隔的字段。

至于答案,我也尝试了下面的查询

g.V(4128).repeat(下())。发射()。树()。下一个()

但响应仅返回了ID并且没有解析顶点属性

[
  {
    "4128": {
      "8272": {},
      "4152": {
        "4176": {
          "4104": {},
          "4296": {},
          "8200": {}
        },
        "8344": {
          "8224": {},
          "8392": {}
        }
      },
      "8248": {},
      "12488": {}
    }
  }
]


对于上下文,我在gremlin-server.yaml文件中使用以下配置

authentication: {className: org.apache.tinkerpop.gremlin.server.auth.AllowAllAuthenticator,
  config: null}
channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
graphs: {graph: 'D:/graph-data-access/src/test/resources/titan-inmemory.properties'}
gremlinPool: 8
host: localhost
maxAccumulationBufferComponents: 1024
maxChunkSize: 8192
maxContentLength: 65536
maxHeaderSize: 8192
maxInitialLineLength: 4096
metrics:
  consoleReporter: null
  csvReporter: null
  gangliaReporter: null
  graphiteReporter: null
  jmxReporter: null
  slf4jReporter: {enabled: true, interval: 180000, loggerName: org.apache.tinkerpop.gremlin.server.Settings$Slf4jReporterMetrics}
plugins: [aurelius.titan, tinkerpop.gephi]
port: 8182
processors: []
resultIterationBatchSize: 64
scriptEngines:
  gremlin-groovy:
    config: null
    imports: [java.lang.Math]
    scripts: ['D:/graph-data-access/src/test/resources/generate-locations.groovy']
    staticImports: [java.lang.Math.PI]
scriptEvaluationTimeout: 30000
serializedResponseTimeout: 30000
serializers:
- className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0
  config: {useMapperFromGraph: graph}
- className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0
  config: {serializeResultToString: true}
- className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0
  config: {useMapperFromGraph: graph}
- className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0
  config: {useMapperFromGraph: graph}
ssl: {enabled: false, keyCertChainFile: null, keyFile: null, keyPassword: null, trustCertChainFile: null}
threadPoolBoss: 1
threadPoolWorker: 1
writeBufferHighWaterMark: 65536
writeBufferLowWaterMark: 32768

我正在使用Tinkerpop 3.0.1版孵化,因为我正在尝试使用Titan 1.0.0和DynamoDb作为远程连接的存储后端。

1 个答案:

答案 0 :(得分:1)

不确定你的意思"但我没有得到有效的json"。如果您不包含Tree并且只是序列化整个顶点,那么您应该能够在GraphSON 1.0中序列化by(valueMap(true))。如果您尝试将其作为地图进行操作,则会出现故障,因为它会对图形元素作为关键字做出一些假设(这可能很糟糕)。

gremlin> mapper = graph.io(graphson()).mapper().version(GraphSONVersion.V1_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@6b2e0f78
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().next())
==>{"1":{"key":{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},"value":{"2":{"key":{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},"value":{}},"3":{"key":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"value":{}},"4":{"key":{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},"value":{"3":{"key":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"value":{}},"5":{"key":{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},"value":{}}}}}}}

格式化为:

{
    "1": {
        "key": {
            "id": 1,
            "label": "vertex",
            "type": "vertex",
            "properties": {
                "name": [{
                    "id": 0,
                    "value": "marko"
                }],
                "age": [{
                    "id": 2,
                    "value": 29
                }]
            }
        },
        "value": {
            "2": {
                "key": {
                    "id": 2,
                    "label": "vertex",
                    "type": "vertex",
                    "properties": {
                        "name": [{
                            "id": 3,
                            "value": "vadas"
                        }],
                        "age": [{
                            "id": 4,
                            "value": 27
                        }]
                    }
                },
                "value": {}
            },
            "3": {
                "key": {
                    "id": 3,
                    "label": "vertex",
                    "type": "vertex",
                    "properties": {
                        "name": [{
                            "id": 5,
                            "value": "lop"
                        }],
                        "lang": [{
                            "id": 6,
                            "value": "java"
                        }]
                    }
                },
                "value": {}
            },
            "4": {
                "key": {
                    "id": 4,
                    "label": "vertex",
                    "type": "vertex",
                    "properties": {
                        "name": [{
                            "id": 7,
                            "value": "josh"
                        }],
                        "age": [{
                            "id": 8,
                            "value": 32
                        }]
                    }
                },
                "value": {
                    "3": {
                        "key": {
                            "id": 3,
                            "label": "vertex",
                            "type": "vertex",
                            "properties": {
                                "name": [{
                                    "id": 5,
                                    "value": "lop"
                                }],
                                "lang": [{
                                    "id": 6,
                                    "value": "java"
                                }]
                            }
                        },
                        "value": {}
                    },
                    "5": {
                        "key": {
                            "id": 5,
                            "label": "vertex",
                            "type": "vertex",
                            "properties": {
                                "name": [{
                                    "id": 9,
                                    "value": "ripple"
                                }],
                                "lang": [{
                                    "id": 10,
                                    "value": "java"
                                }]
                            }
                        },
                        "value": {}
                    }
                }
            }
        }
    }
}

在GraphSON 2.0中,你得到了这个:

gremlin> mapper = graph.io(graphson()).mapper().version(GraphSONVersion.V2_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@3ffb3598
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().next())
==>{"@type":"g:Tree","@value":[{"key":{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":1},"label":"vertex","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":0},"value":"marko","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":2},"value":{"@type":"g:Int32","@value":29},"label":"age"}}]}}},"value":{"@type":"g:Tree","@value":[{"key":{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":2},"label":"vertex","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":3},"value":"vadas","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":4},"value":{"@type":"g:Int32","@value":27},"label":"age"}}]}}},"value":{"@type":"g:Tree","@value":[]}},{"key":{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":3},"label":"vertex","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":5},"value":"lop","label":"name"}}],"lang":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":6},"value":"java","label":"lang"}}]}}},"value":{"@type":"g:Tree","@value":[]}},{"key":{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":4},"label":"vertex","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":7},"value":"josh","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":8},"value":{"@type":"g:Int32","@value":32},"label":"age"}}]}}},"value":{"@type":"g:Tree","@value":[{"key":{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":3},"label":"vertex","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":5},"value":"lop","label":"name"}}],"lang":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":6},"value":"java","label":"lang"}}]}}},"value":{"@type":"g:Tree","@value":[]}},{"key":{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":5},"label":"vertex","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":9},"value":"ripple","label":"name"}}],"lang":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int32","@value":10},"value":"java","label":"lang"}}]}}},"value":{"@type":"g:Tree","@value":[]}}]}}]}}]}

格式为:

{
    "@type": "g:Tree",
    "@value": [{
        "key": {
            "label": "vertex",
            "name": ["marko"],
            "id": {
                "@type": "g:Int32",
                "@value": 1
            },
            "age": [{
                "@type": "g:Int32",
                "@value": 29
            }]
        },
        "value": {
            "@type": "g:Tree",
            "@value": [{
                "key": {
                    "label": "vertex",
                    "name": ["lop"],
                    "id": {
                        "@type": "g:Int32",
                        "@value": 3
                    },
                    "lang": ["java"]
                },
                "value": {
                    "@type": "g:Tree",
                    "@value": []
                }
            }, {
                "key": {
                    "label": "vertex",
                    "name": ["vadas"],
                    "id": {
                        "@type": "g:Int32",
                        "@value": 2
                    },
                    "age": [{
                        "@type": "g:Int32",
                        "@value": 27
                    }]
                },
                "value": {
                    "@type": "g:Tree",
                    "@value": []
                }
            }, {
                "key": {
                    "label": "vertex",
                    "name": ["josh"],
                    "id": {
                        "@type": "g:Int32",
                        "@value": 4
                    },
                    "age": [{
                        "@type": "g:Int32",
                        "@value": 32
                    }]
                },
                "value": {
                    "@type": "g:Tree",
                    "@value": [{
                        "key": {
                            "label": "vertex",
                            "name": ["ripple"],
                            "id": {
                                "@type": "g:Int32",
                                "@value": 5
                            },
                            "lang": ["java"]
                        },
                        "value": {
                            "@type": "g:Tree",
                            "@value": []
                        }
                    }, {
                        "key": {
                            "label": "vertex",
                            "name": ["lop"],
                            "id": {
                                "@type": "g:Int32",
                                "@value": 3
                            },
                            "lang": ["java"]
                        },
                        "value": {
                            "@type": "g:Tree",
                            "@value": []
                        }
                    }]
                }
            }]
        }
    }]
}

请注意,对于GraphSON 2.0,您还可以使用by(valueMap(true))

gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().by(valueMap(true)).next())
==>{"@type":"g:Tree","@value":[{"key":{"label":"vertex","name":["marko"],"id":{"@type":"g:Int32","@value":1},"age":[{"@type":"g:Int32","@value":29}]},"value":{"@type":"g:Tree","@value":[{"key":{"label":"vertex","name":["lop"],"id":{"@type":"g:Int32","@value":3},"lang":["java"]},"value":{"@type":"g:Tree","@value":[]}},{"key":{"label":"vertex","name":["vadas"],"id":{"@type":"g:Int32","@value":2},"age":[{"@type":"g:Int32","@value":27}]},"value":{"@type":"g:Tree","@value":[]}},{"key":{"label":"vertex","name":["josh"],"id":{"@type":"g:Int32","@value":4},"age":[{"@type":"g:Int32","@value":32}]},"value":{"@type":"g:Tree","@value":[{"key":{"label":"vertex","name":["ripple"],"id":{"@type":"g:Int32","@value":5},"lang":["java"]},"value":{"@type":"g:Tree","@value":[]}},{"key":{"label":"vertex","name":["lop"],"id":{"@type":"g:Int32","@value":3},"lang":["java"]},"value":{"@type":"g:Tree","@value":[]}}]}}]}}]}

格式为:

{
    "@type": "g:Tree",
    "@value": [{
        "key": {
            "label": "vertex",
            "name": ["marko"],
            "id": {
                "@type": "g:Int32",
                "@value": 1
            },
            "age": [{
                "@type": "g:Int32",
                "@value": 29
            }]
        },
        "value": {
            "@type": "g:Tree",
            "@value": [{
                "key": {
                    "label": "vertex",
                    "name": ["lop"],
                    "id": {
                        "@type": "g:Int32",
                        "@value": 3
                    },
                    "lang": ["java"]
                },
                "value": {
                    "@type": "g:Tree",
                    "@value": []
                }
            }, {
                "key": {
                    "label": "vertex",
                    "name": ["vadas"],
                    "id": {
                        "@type": "g:Int32",
                        "@value": 2
                    },
                    "age": [{
                        "@type": "g:Int32",
                        "@value": 27
                    }]
                },
                "value": {
                    "@type": "g:Tree",
                    "@value": []
                }
            }, {
                "key": {
                    "label": "vertex",
                    "name": ["josh"],
                    "id": {
                        "@type": "g:Int32",
                        "@value": 4
                    },
                    "age": [{
                        "@type": "g:Int32",
                        "@value": 32
                    }]
                },
                "value": {
                    "@type": "g:Tree",
                    "@value": [{
                        "key": {
                            "label": "vertex",
                            "name": ["ripple"],
                            "id": {
                                "@type": "g:Int32",
                                "@value": 5
                            },
                            "lang": ["java"]
                        },
                        "value": {
                            "@type": "g:Tree",
                            "@value": []
                        }
                    }, {
                        "key": {
                            "label": "vertex",
                            "name": ["lop"],
                            "id": {
                                "@type": "g:Int32",
                                "@value": 3
                            },
                            "lang": ["java"]
                        },
                        "value": {
                            "@type": "g:Tree",
                            "@value": []
                        }
                    }]
                }
            }]
        }
    }]
}

如果你使用旧版本的TinkerPop回到3.0.x,那么树的一些限制并不容易解决。唯一的方法是采用原始树并对其进行后处理,以使其成为对JSON友好的表单。这是一种方法:

gremlin> convert = { it.collectEntries{ k,v -> [(k.id()): [k, v.isEmpty() ? v : convert(v)]] }}
==>groovysh_evaluate$_run_closure1@255e5e2e
gremlin> t = g.V(1).repeat(out()).emit().tree().next()
==>v[1]={v[2]={}, v[3]={}, v[4]={v[3]={}, v[5]={}}}
gremlin> convert(t)
==>1=[v[1], {2=[v[2], {}], 3=[v[3], {}], 4=[v[4], {3=[v[3], {}], 5=[v[5], {}]}]}]
gremlin> mapper.writeValueAsString(convert(t))
==>{"1":[{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},{"2":[{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},{}],"3":[{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},{}],"4":[{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},{"3":[{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},{}],"5":[{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},{}]}]}]}

这种格式在GraphSON 1.0(3.0.x中使用的)中:

{
    "1": [{
        "id": 1,
        "label": "vertex",
        "type": "vertex",
        "properties": {
            "name": [{
                "id": 0,
                "value": "marko"
            }],
            "age": [{
                "id": 2,
                "value": 29
            }]
        }
    }, {
        "2": [{
            "id": 2,
            "label": "vertex",
            "type": "vertex",
            "properties": {
                "name": [{
                    "id": 3,
                    "value": "vadas"
                }],
                "age": [{
                    "id": 4,
                    "value": 27
                }]
            }
        }, {}],
        "3": [{
            "id": 3,
            "label": "vertex",
            "type": "vertex",
            "properties": {
                "name": [{
                    "id": 5,
                    "value": "lop"
                }],
                "lang": [{
                    "id": 6,
                    "value": "java"
                }]
            }
        }, {}],
        "4": [{
            "id": 4,
            "label": "vertex",
            "type": "vertex",
            "properties": {
                "name": [{
                    "id": 7,
                    "value": "josh"
                }],
                "age": [{
                    "id": 8,
                    "value": 32
                }]
            }
        }, {
            "3": [{
                "id": 3,
                "label": "vertex",
                "type": "vertex",
                "properties": {
                    "name": [{
                        "id": 5,
                        "value": "lop"
                    }],
                    "lang": [{
                        "id": 6,
                        "value": "java"
                    }]
                }
            }, {}],
            "5": [{
                "id": 5,
                "label": "vertex",
                "type": "vertex",
                "properties": {
                    "name": [{
                        "id": 9,
                        "value": "ripple"
                    }],
                    "lang": [{
                        "id": 10,
                        "value": "java"
                    }]
                }
            }, {}]
        }]
    }]
}

我创建的convert()函数只是递归地迭代树,并将每个[vertex: children]转换为[vertexId: [vertex,children]。您可以转换为您喜欢的任何格式,只要您构建有效的JSON,其中有一个可用的String键字段。我可以轻松地使convert()函数返回[vertexId: [node: vertex, leaves: children]

gremlin> convert = { it.collectEntries{ k,v -> [(k.id()): [node: k, leaves:v.isEmpty() ? v : convert(v)]] }}
==>groovysh_evaluate$_run_closure1@2ab5afc7
gremlin> mapper.writeValueAsString(convert(t))
==>{"1":{"node":{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},"leaves":{"2":{"node":{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},"leaves":{}},"3":{"node":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"leaves":{}},"4":{"node":{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},"leaves":{"3":{"node":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"leaves":{}},"5":{"node":{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},"leaves":{}}}}}}}