union步骤做的工作就是合并,这与你从它的名称所期待的一样。它允许您把查询结果的多个部分合并到单一结果中。就像使用布尔运算and, or步骤一样,有时您可以找到其它的方式来做相同的事情,不需要用到union步骤,但是它确实是一种很有用的步骤。这是一个使用union步骤产生顶点和顶点出边数列表的一个例子。注意在下一节,我们可以看到有更简洁的方法来写这个查询,它也用到了union步骤。这个例子我们主要关注的是:您可以用一个union步骤来把多个遍历的结果合并。这个例子合并了两个遍历的结果,如果需要,您其实可以合并更多个。注意union步骤之前,out步骤从最先找到的顶点开始,在这个例子中是DFW 顶点。所以换句话说,前一个步骤的输出可以用在union步骤中,这和我们已经学习过的小精灵的其它步骤一样。

The union step works just as you would expect from its name. It allows us to combine parts of a
query into a single result. Just as with the boolean and and or steps it is sometimes possible to find
other ways to do the same thing without using union but it does offer some very useful capability.
Here is a simple example that uses a union step to produce a list containing a vertex and the
number of outgoing routes from that vertex. Note that in the next section we will see that there are
simpler ways to write this query while still using a union step. The main point to take away from
this example is that you can use a union step to combine the results of multiple traversals. This
example combines the results of two traversals but you can certainly combine more as needed.
Note that the out step starts from the vertex that was found immediately before the union step
which in this case is the DFW vertex. So in other words the output from the prior step is available to
the steps within the union step just as with other Gremlin steps we have already looked at.

 

前边的查询也可以写成用一个union步骤合并两个has步骤,这个不是特别的推荐。然而这个例子也说明了您可以用一个union步骤来把任意图遍历的结果合并起来。

Not that this is recommended, but the previous query could also be written as follows using two has
steps both inside a single union step. This does however demonstrate that you can use a union step
to combine the results of fairly arbitrary graph traversals.
 

 

顺带说一下,也可以不使用union步骤,就生成一个列表。我们可以使用group步骤产生一个表map.如果您想单独访问访问键或者值,表map是不错的方案。总之这都取决于您要解决的问题适合哪种结果。

As a side note, instead of using a union step and producing a list, we might decide to use a group
step and produce a map. A map might be preferable if you want to access individual keys and
values directly. It all depends, as always, on the results that best fit the problem you are solving.

 

 

posted on 2022-04-19 15:59  bokeyuannicheng0000  阅读(61)  评论(0)    收藏  举报