我有以下查询:
g
.V("user-11")
.repeat(bothE().subgraph("subGraph").outV())
.times(2)
.cap("subGraph")
.next()当我使用gremlin-python运行它时,我会收到以下响应:
{'@type': 'tinker:graph',
'@value': {'vertices': [v[device-3], v[device-1], v[user-11], v[card-1]],
'edges': [e[68bad734-db2b-bffc-3e17-a0813d2670cc][user-11-uses_device->device-1],
e[14bad735-2b70-860f-705f-4c0b769a7849][user-11-uses_device->device-3],
e[f0bb3b6d-d161-ec60-5e6d-068272297f24][user-11-uses_card->card-1]]}}它是查询获得的子图的Graphson表示形式。
我想使用和gremlin-driver获得相同的响应,但我一直无法理解如何实现。
我最大的努力是:
ObjectMapper mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).create().createMapper();
Object a = graphTraversalSource
.V(nodeId)
.repeat(bothE().subgraph("subGraph").outV())
.times(2)
.cap("subGraph")
.next();
return mapper.writeValueAsString(a);但这给了我以下错误:
io.netty.handler.codec.DecoderException: org.apache.tinkerpop.gremlin.driver.ser.SerializationException: org.apache.tinkerpop.shaded.kryo.KryoException: Encountered unregistered class ID: 65536我正在使用AWS海王星,但考虑到我通过gremlin-python收到了我想要的答案,我怀疑这会有什么不同。
我很感激你能给予的任何帮助!谢谢
发布于 2021-01-04 23:01:35
如评论中所述
当使用Java时,您得到的将是一个实际的TinkerGraph
陀螺仪是旧的,如果您没有指定其他序列化程序之一,则可能会导致所看到的错误。
请注意,即使使用其他序列化程序之一,要将图反序列化为JSON,也需要使用特定的TinkerGraph序列化程序(示例请参阅此答案的结尾)。否则,您将只需要返回{}。
但是,在Java Gremlin客户端中,您可能根本不需要生成JSON .
如果您有一个实际的TinkerGraph返回,您可以对内存中的子图运行真正的Gremlin查询--只需为它创建一个新的遍历源。如果您愿意,还可以使用graph.io类将图形写入文件。TinkerGraph将包括属性以及边和顶点。
还可以使用以下语句直接访问TinkerGraph对象
a.vertices和a.edges
通过一个具体的例子,如果您对表单有一个查询
TinkerGraph tg = (TinkerGraph)g.V().bothE().subgraph("sg").cap("sg").next();那你就可以
GraphTraversalSource g2 = tg.traversal();
Long cv = g2.V().count().next();
Long ce = g2.E().count().next();也可以直接使用表单的语句访问TinkerGraph数据结构:
Vertex v = tg.vertices[<some-id>]或
List properties = tg.vertices[<some-id>].properties()这实际上意味着在Java客户端处理子图时,您可以使用更多的功能。
如果您仍然觉得您需要一个JSON版本的子图,IO引用是一个方便的书签:https://tinkerpop.apache.org/docs/3.4.9/dev/io/#_io_reference
编辑:-为了节省大量阅读文档,这段代码将将TinkerGraph打印为JSON
mapper = GraphSONMapper.build().
addRegistry(TinkerIoRegistryV3d0.instance()).
version(GraphSONVersion.V3_0).create().createMapper();
mapper.writeValueAsString(tg) https://stackoverflow.com/questions/65570629
复制相似问题