我正在使用Gremlin-Python Client查询带有janusgraph后端的Gremlin Server。
运行以下查询:
graph = Graph()
g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))
sg = g.E().subgraph('a').cap('a').next()
查询返回一个包含边和顶点列表的子图。
我在服务器上配置了以下序列化器
serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}
有人知道如何配置gremlin-server和示例代码以返回完整 填充子图?
根据斯蒂芬的反馈更新了测试用例
# DB: Janusgraph with Opensource Cassandra storage backend
# Data: v[41427160]--reports_to-->v[36712472]--reports_to-->v[147841048]
# Objective: get subgraph detached to python client with all properties of the vertex and edges
(py365)$ pip list | grep gremlinpython
gremlinpython 3.3.4
(py365)$ python
Python 3.6.5 (default, Apr 25 2018, 14:26:36)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gremlin_python.driver import client
>>> from gremlin_python.driver.serializer import GraphSONSerializersV3d0
>>> session = client.Client('ws://localhost:8182/gremlin', 'g', message_serializer=GraphSONSerializersV3d0())
>>> query_parameters = {"vids": [41427160, 36712472]}
>>> query = "g.V(vids).outE('reports_to').subgraph('1').otherV().cap('1').next()"
>>> results = session.submit(query, query_parameters)
>>> for r in results:
... r_vertices = r[0]['@value'].get('vertices')
... r_edges = r[0]['@value'].get('edges')
... print(r)
... print(r_vertices)
... print(r_edges)
...
[{'@type': 'tinker:graph', '@value': {'vertices': [v[41427160], v[147841048], v[36712472]], 'edges': [e[{'@type': 'janusgraph:RelationIdentifier', '@value': {'relationId': '21y8ez-onxeg-f11-luviw'}}][41427160-reports_to->36712472], e[{'@type': 'janusgraph:RelationIdentifier', '@value': {'relationId': '225dz7-luviw-f11-2g0qvs'}}][36712472-reports_to->147841048]]}}]
[v[41427160], v[147841048], v[36712472]]
[e[{'@type': 'janusgraph:RelationIdentifier', '@value': {'relationId': '21y8ez-onxeg-f11-luviw'}}][41427160-reports_to->36712472], e[{'@type': 'janusgraph:RelationIdentifier', '@value': {'relationId': '225dz7-luviw-f11-2g0qvs'}}][36712472-reports_to->147841048]]
>>>
gremlinpython是否确实是轻量级的,即使使用 基于脚本的方法,只有必要的元素(id和label)是 作为图子的“参考元素” 部分分开了吗?
答案 0 :(得分:4)
使用Gremlin Python(或与此相关的任何其他语言变体)不能完全将subgraph()
步骤的结果作为Graph
返回。问题在于Gremlin Python是Gremlin的轻量级实现,因此没有图形数据结构实例可将返回的数据反序列化到其中。
这时,唯一的解决方法是简单地返回构成图形的数据,然后您必须将该数据存储到Python中的图形中。所以也许您会这样做:
g.E().project('edgeId','label','inId','outId').
by(id).
by(label).
by(inV().id()).
by(outV().id())
这将以Map
的形式返回子图结构所需的最少数据,然后您可以在Python中使用该数据做一些事情。
我认为不建议使用的另一个选项是使用Python提交脚本,而不是使用基于字节码的请求。使用脚本,您将获得子图的GraphSON表示,然后可以根据需要将其解析为Python中的某些数据结构。这相当于您需要发送的脚本:
gremlin> graph = g.E().hasLabel('knows').subgraph('sg').cap('sg').next()
==>tinkergraph[vertices:3 edges:2]
gremlin> mapper = GraphSONMapper.build().addRegistry(TinkerIoRegistryV3d0.instance())create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@f6de586
gremlin> mapper.writeValueAsString(graph)
==>{"@type":"tinker:graph","@value":{"vertices":[{"@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"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":2},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":2},"value":"vadas","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":3},"value":{"@type":"g:Int32","@value":27},"label":"age"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":4},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":6},"value":"josh","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":7},"value":{"@type":"g:Int32","@value":32},"label":"age"}}]}}}],"edges":[{"@type":"g:Edge","@value":{"id":{"@type":"g:Int32","@value":7},"label":"knows","inVLabel":"person","outVLabel":"person","inV":{"@type":"g:Int32","@value":2},"outV":{"@type":"g:Int32","@value":1},"properties":{"weight":{"@type":"g:Property","@value":{"key":"weight","value":{"@type":"g:Double","@value":0.5}}}}}},{"@type":"g:Edge","@value":{"id":{"@type":"g:Int32","@value":8},"label":"knows","inVLabel":"person","outVLabel":"person","inV":{"@type":"g:Int32","@value":4},"outV":{"@type":"g:Int32","@value":1},"properties":{"weight":{"@type":"g:Property","@value":{"key":"weight","value":{"@type":"g:Double","@value":1.0}}}}}}]}}
我们将重新考虑子版本在TinkerPop的未来版本中如何适用于不同的语言变体,但目前,这是我们仅有的解决方案。
答案 1 :(得分:0)
上述解决方案对我不起作用。 使用GremlinPython中的SubgraphStrategy更好的方法:
sg = g.withStrategies(SubgraphStrategy())
sg将是GraphTraversalSource。.check official documentation
例如,如果我想找到带有顶点标签“ person”的子图
sg = g.withStrategies(SubgraphStrategy(vertices=hasLabel('person')))