通过回顾以前的一些生成集合的方式来开始这部分知识的讨论。如下面的例子所示,通过group步骤来生成map是最简单的例子之一。
Let’s start this discussion by first reviewing a few ways that a collection can be generated. A simple
example of a collection is the map that is generated by the group step as shown in the example
below.
类似地,也可使用groupCount步骤来创建一个map表。
Similarly a map is created when the groupCount step is used.
相似地,当我们使用fold步骤时也可以生成一个列表。为了让列表的内容有序,我们用到了local范围和order步骤。
Likewise, when we use the fold step a list is generated. We can use the order step with a local scope
to order the contents of the list.
这是一个使用union步骤的例子,它后边跟着一个fold步骤,生成了一个有两个值的列表。 这个union步骤包含了一个identity步骤表示我们想把入顶点的传下做为union的首项。我们把这个顶点和从这个顶点DFW出发的所有路径的总数合在一起,最后使用一个fold步骤生成了一个列表。
Here is an example of a union step followed by a fold step that generates a list of two values. The
union step contains an identity step to indicate that we want the value of the incoming vertex as the
first item of the union. We union that vertex with a count of all routes from that vertex (DFW) and
finally use a fold step to generate a list.
注意上面的句法,它是一种下面这条查询的简写的形式。
Note that the above syntax, is a shorthand form of the following.
如果我们想生成的是一个有键和值的表而不是一个列表。我们可以使用group步骤。在这个例子中,顶点是键,出边的数量是值。
If we wanted to generate a map with keys and values rather than a list, we could use a group step.
In this case the vertex is the keys and the number of outgoing routes is the value.
另一种创建表的方式是使用project步骤。
Another way that a map can be created is when the project step is used.
当然使用project步骤会有点复杂,这个例子创建了一个有两个键的表map.第一个键是dfw,它包含了DFW机场顶点,第二个键是route_count它包含的数字是从DFW出发的出边的数量。注意第一个by步骤没有参数,所以它返回的是真实的顶点(而不是来自顶点的我们可选的属性)。
Also using a project step, but a little more complex, this example creates a map with two keys. The
first, called dfw, will contain the vertex for the DFW airport and the second, called route_count, will
contain the number of outgoing routes from DFW. Notice how the first by step has no parameters so
it returns the actual vertex (rather than say a property from the vertex that we could select).
和生成表map一样,我们也可以生成集合,这时用到一个store步骤,这样就不会存重复的值了。withSideEffect步骤也可以用于初始化一个集合。cap步骤是从我们用store步骤创建的side effect中得到的集合结果发布出来。它允许我们返回这个集合作为查询的最终结果。
As well as generating maps, we can also generate a set using a store step so that duplicate values
are not stored. The withSideEffect step can be used to initialize the set. The cap step emits the
collection resulting from the side effect that we created using the store step. Among other things
this allows us to return this collection as the final result of a query.
store步骤可以和by调节器一起使用指定存储什么。下面的查询用到了个store步骤创建了一个跑道的集合,但是不需要使用 values步骤。
The store step can also be used in conjunction with a by modulator to specify exactly what is stored.
The query below uses a store step to create a collection of runways but avoids the need to use a
values step.
aggregate 步骤也可以生成集合,像下面这样。这个集合事实上是BulkSet,我们可以确认。store步骤也生成了一个BulkSet。
The aggregate step also generates a collection as shown below. The collection is actually a BulkSet as
we shall confirm shortly. The store step also generates a BulkSet.
下面的查询用一个aggregate步骤找到从Ireland的机场出发可以到达的所有的国家,但是排除在Ireland内的机场的航班。
The query below uses an aggregate step to find all the countries that you can fly to from airports in
Ireland but excludes routes that are between airports within Ireland.
从结果中您可以看到,国家代码IE没有出现在列表中。
As you can see by looking at the results, the country code for Ireland, IE, is not present in the list.
aggregate和store步骤表面上看起来一样,事实上它们的行为是不同的。aggregate步骤会阻塞并立即收集前边的遍历的每样东西,而store步骤只是把东西加到集合中。这有时被称为是“懒聚合”。注意即使人们指定了limit(2),store步骤还是会收集三个元素。因为第三个在limt步骤使用之前,已经被看见了。
While aggregate and store on the surface appear identical, they actually behave differently. The
aggregate step will block and immediately gather up everything from the prior traversal, whereas
the store step will only add things to its collection as they are seen. This is sometimes referred to as
lazy aggregation. Note also that even though we specified a limit of 2, the store step collected three
elements as the third has already been seen before the limit step is applied.
aggregate 和store都可以用一个by调节器来更精确地指出收集哪些东西。例如,如果我们想要收集在Ireland的每个机场的跑道数,我们可以像下面这么做。
Both aggregate and store can be followed by a by modulator to specify more precisely what should
be collected. For example, if we wanted to store the number of runways that each airport in Ireland
has we could do so as follows.
如果我们不确定什么类型的对象被创建了,我们可以调用getClass方法来弄明白。
If we are ever unsure what type of object has been created a call to getClass can be used to find out.
我们已经解释了各种在一个遍历中生成集合的方式。重要的是理解如何访问和操纵集合的内容。
Now that we have examined the various ways in which collections may get generated during a
traversal, it is important to understand how the contents of a collection can be accessed and
manipulated.