当您看一看小精灵用户讨论列表中常被问到的写小精灵查询的问题的时候,一个反复出现的问题看起来引起了人们的困惑,那就是确定遍历步骤的行为,也就是消除障碍的步骤。这些步骤实质上就是消除遍历的结果这样就只有一个遍历了。常常只有一个值,或者某种集合,并且从那个时候开始,您不可能引用到在查询中您以前可以引用的东西。
If you look at commonly asked questions about writing Gremlin queries on the Gremlin Users
discussion list, one area that repeatedly seems to cause people confusion is the behavior of certain
traversal steps that are known as reducing barrier steps. What these steps in essence do is reduce
the results of the traversal so far to a single traversal, often just a value or a collection of some kind
and from that point on you cannot refer back to things you did earlier in the query.
看下面这个例子。表面上看,您可能想让查询统计所有从DFW机场出发的航线,并返回统计结果b 和机场顶点a.
Take a look at the example below. On the surface, you might expect the query to count all of the
routes originating from the DFW airport and return the count "b" along with the airport vertex "a".
实际上什么都没返回。这是因为count步骤也被称为是"消除障碍步骤",一旦count被执行,您将跨越了障碍,遍历变量将不再可用。我们仍可以访问变量b,如果我们通过它自身引用它,这晨因为它是定义在count步骤之后的,如下所示。
What actually happens is that nothing is returned. This is because the count step is a so called
reducing barrier step. Once the count has been processed, you have crossed the barrier and the
traversal variable "a" is no longer available to us. We can still access "b" if we reference it by itself
as it is defined after the count step as shown below.
在像这样的例子中,通过改变您写查询的方式,总是可以得到您所需要的结果的。重要的是理解不同的遍历步骤是如何工作的。最好的方法就是在小精灵控制台做实验。看一年不同步骤的运行。重写的查询如下,得到了我们原定的目标。
In cases such as this, it is almost always possible to achieve the results that you want by changing
the way you write the query. It is important to gain an understanding of how different traversal
steps work. A great way to do that is to experiment using the Gremlin Console and look at the way
different steps operate. The rewritten query below achieves our original goal.
如果这是我们需要的全部,我们的工作可以这么做。我们有一个map包含了顶点做为键,它的出边数做为值。然则仍有一个问题,如果我们想要更进一点的进行这个查询。看下面这个例子。因为查询把前边的遍历放到了一个小map中,包含一个统计count, 我们就不能再引用a了。
If this was all we needed then our job is done. We have a map containing the vertex as the key and
its outgoing route count as the value. However, there is still an issue if we want to go further with
this query. Take a look at the example below. Because the query has reduced the prior traversal to
essentially a small map, including a count, we can no longer refer back to "a".
出于一些原因我们想要获取我们存在a中的顶点,我们可以把它从group步骤创建的map中拉出来。您可以使用keys关键字做为select步骤的参数来访问map的键。
If for some reason, we wanted to retrieve the vertex that we had stored in "a", we should instead
pull it from the map that the group step created. You can access the keys of a map using the keys
keyword as a parameter to a select step.
我们还是没有真正得到我们期待的结果,因为这个顶点仍然是在一个列表中。所以我们可以再修改一下查询,在我们从map中选择keys之前,去解绑map。
We have still not quite got the result we wanted as the vertex is still returned in a list. So we can
modify the query again to unfold the map before we select the keys from it.
如果我们想要得到的是值而不是键,我们可以像下面这样用到values关键字。
If we wanted to get the value back instead of the key we can use the values keyword as follows.
为了证明我们可以进一步的处理查询,让我们把我们出发的机场的代码返回。
To prove we could carry on adding to the query from here let’s get back the airport code that we
started with.
所以,现在我们在查询中增加并创建一个带dfwcount标签的新顶点,它将使用一个称为curretn_count的属性来存储DFW机场的路径数,
So, lets now add to our query and create a new vertex with a label dfwcount that is going to store
the number of routes originating in DFW using a property called current_count.
我们可以检查新的顶点,来验证我们的查询已经奏效了。
We can inspect the new vertex to double check that our query worked as intended.
相信您看到了这的一个样式。重要的是理解哪些步骤是消除障碍的步骤。能够使用它们,让您编写的查询按您的需要工作。
Hopefully you are starting to see a pattern here. It is important to understand which steps are
reducing barrier steps and be able to work with them in a way that allows you to write queries that
do what you need.