简体   繁体   English

TinkerPop 获取计数并继续相同的遍历

[英]TinkerPop get the count and continue the same traversal

I'm currently using TinkerPop java APIs for graph traversal.我目前正在使用 TinkerPop java API 进行图遍历。 Currently I had to create a duplicate copy of the same traversal to compute the count.目前,我必须创建相同遍历的副本来计算计数。

Long allUsersCount = gt.V().hasLabel("user").has("name", "John").count().next();
List<Vertex> users = gt.V().hasLabel("user").has("name", "John").toList();

When I tried to reuse the traversal returned by gt.V().hasLabel("user").has("name", "John").count() to get the list, it caused the error当我尝试重用gt.V().hasLabel("user").has("name", "John").count()返回的遍历来获取列表时,它导致了错误

java.lang.IllegalStateException: The traversal strategies are complete and the traversal can no longer be modulated java.lang.IllegalStateException:遍历策略完成,无法再调制遍历

I just want to know if there is any way to avoid this repetition as the traversal is same for both cases gt.V().hasLabel("user").has("name", "John") just the terminating operations are different.我只想知道是否有任何方法可以避免这种重复,因为这两种情况的遍历是相同的gt.V().hasLabel("user").has("name", "John")只是终止操作是不同的。

Is there any way to store the count in-between(inside a java variable) and continue the traversal to get the list of users.有什么方法可以存储中间的计数(在 java 变量中)并继续遍历以获取用户列表。

The count() step is a "reducing barrier step" which is why you see the behavior you do. count()步骤是“减少障碍步骤”,这就是您看到自己所做行为的原因。 There are many ways to work around this.有很多方法可以解决这个问题。 Here is one example that uses the air-routes data set.这是一个使用航线数据集的示例。

gremlin> g.V().has('city','London').
               fold().
               project('cities','count').
                 by().
                 by(count(local))

==>[cities:[v[49],v[50],v[88],v[94],v[206],v[649]],count:6]    

You can achieve this by projecting your results and using a count() step with a local scope.您可以通过投影结果并使用带有本地 scope 的 count() 步骤来实现此目的。

g.V().fold().project('cnt', 'edges').by(count(local)).by()

returns this:返回这个:

==>[cnt:6,edges:[v[1],v[2],v[3],v[4],v[5],v[6]]]

Local scope (ie count(local)) will perform the desired operation on each list inside the current object.本地 scope(即 count(local))将对当前 object 内的每个列表执行所需的操作。 In this case we are finding all vertices, collecting them into a list.在这种情况下,我们要查找所有顶点,并将它们收集到一个列表中。 Once we are in the project() we are then counting the local scope (ie the number of items in the list) and returning that count along with the original list.一旦我们在 project() 中,我们就会计算本地 scope(即列表中的项目数)并将该计数与原始列表一起返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM