从顶点列表创建新的遍历对象

Create new traversal object from a list of vertices

我想过滤我的图表以仅包含边数小于阈值(例如 50)的顶点,如下所示:

g.V().filter(bothE().limit(50).count().is(lt(50)))

这给了我要保留的顶点列表。

如何创建仅包含这些顶点的 traversal 对象?

背景

我需要计算图中每个顶点的 k-hop 邻域,它过滤掉具有大量边(例如 <50)的顶点。过滤后的图有几百万条边和顶点。

想到的第一种方法是首先过滤图,将结果存储为新的子图,然后遍历每个顶点以找到 k-hop 邻域。对于单个顶点v,k=5-hop邻域编码为:

g.V(v).repeat(__.bothE().bothV()).times(5).dedup().toList()

更好的方法可能是迭代原始的、未过滤的图中的每个顶点并忽略连接到高边数顶点的边,但我不太确定如何执行此操作。

尝试 1:

filtered_edges = g.V().filter(bothE().limit(50).count().is_(lt(50))).outE().toList()
subgraph = g.E(filtered_edges).subgraph('subGraph').cap('subGraph').next()

不幸的是,当使用 gremlinpython 时会抛出一个错误 (StreamClosedError: Stream is closed)。 运行 其他 - 可能更便宜 - 出现此错误之前和之后的查询不会产生类似的错误,因此与 gremlin shell 的连接仍然存在。该代码也适用于 gremlin shell(将 is_ 替换为 is)。

我想这是因为我在 gremlin 服务器和 Python 之间发送了太多数据,但不确定为什么这会成为问题。

尝试 2:

使用 gremlin 客户端。我尝试用名称 l 覆盖另一个遍历对象。但是,覆盖操作失败了 (l = subgraph.traversal();)。

gremlin_client = client.Client('ws://{}:{}/gremlin'.format('localhost', 8192), 'g', message_serializer=serializer.GraphSONSerializersV3d0())


command = "filtered_edges = g.V().filter(bothE().limit(50).count().is(lt(50))).outE().toList(); subgraph = g.E(filtered_edges).subgraph('subGraph').cap('subGraph').next(); l = subgraph.traversal();"
gremlin_client.submit(command).all().result()

您可以从那里继续遍历:

s.V().filter(bothE().limit(50).count().is(lt(50))).out().has(...)....

或:

List<Vertex> list = s.V().filter(bothE().limit(50).count().is(lt(50))).toList()
s.V(list).out().has(...)....