下面的查询使用sample步骤从图中随机的抽取了任意的10个机场。对于选中的每个机场,一个union合并步骤随后用来把顶点的ID和一些其它属性合并在一起。注意local的作用范围,每个union步骤的结果都封装进了一个列表中。
The following query uses a sample step to select 10 airports at random from the graph. For each
selected airport, a union step is then used to combine the id of the vertex with a few properties.
Note that local scope is used so that the results of each union step are folded into a list.
这是运行查询语句,笔者得到的输出。
Here is the output I got back from running the query.
如果没有使用local,结果将会是一个列表,包含了所有的结果,如下所示。
If local scope had not been used, the result would have been a single list containing all of the results
as shown below.
另一个简单的例子,下面的查询返回了从英国UK出发到达的航班,或者从AUS出发到达墨西哥Mexico的航班。
By way of another simple example, the following query returns flights that arrive in AUS from the
UK or that leave AUS and arrive in Mexico.
我们运行查询,得到了下面的结果,在UK从LHR出发到达墨西哥三个机场MEX,CUN,GDL 的路线。
When we run that query, we get the following results showing that there are routes from LHR in
the UK and to the three airports MEX, CUN and GDL in Mexico.
这个查询解决了“找到所有从英国伦敦出发,直飞到达巴黎或者柏林的路线”因为使用的是城市名而不是机场的代码,需要考虑那个城市所有的机场。
This query solves the problem "Find all routes that start in London, England and end up in Paris or
Berlin with no stops". Because city names are used and not airport codes, all airports in the
respective cities are considered.
运行查询得到的结果如下。注意:找到了伦敦5个不同机场的路线。
Here are the results from running the query. Note that routes from five different London airports
were found.
如上所述,特别是在一些简单的查询中,是存在代替union步骤的方法的。事实上,为了得到前边的结果,没有必要使用union步骤。下面重写的查询语句和使用了union步骤的那一版查询语句一样将返回相同的结果。这次用到的方法是包含within谓词的has步骤。
As mentioned previously, sometimes, especially for fairly simple queries, there are alternatives to
using union. Indeed, it is actually not necessary to use a union step to achieve the prior result. The
re-written version of the query below will return the same results as the version that uses a union
step. This time a simple has step featuring a within predicate is used instead.
前边的两个查询都用了path步骤,来显示每个路径。我们可以修改之前的使用了union步骤的查询语句,把返回结果放在一系列表中,列表中的第一项是出发的机场,列表中的其它项是您可以抵达的三个机场中的一个。在这种方式中用到了双下划线,identity也可用于引用入顶点。union步骤封装在了local步骤中,这样每个union都可以单独封装。如果省略了local步骤,所有的结果都会封装在一个列表中。在这个场景下,union步骤使得查询语句的书写相对容易了,这是一个使用union步骤的一个好的例子。名义上,当您需要合并多个遍历结果时就可以使用union步骤。
The previous two queries used a path step to essentially show each individual route. We can adjust
the version of the query that uses a union step just a little bit and instead turn the result into a
series of lists where the first item in the list is the origin airport and the remaining items in the list
are the places you can fly to from there within our criteria. The double underscore "__" is used in
the way that identity could have been used to refer to the incoming vertex. The union step is
wrapped in a local step so that each union is individually folded. If the local step was omitted all of
the results would be folded into a single list. In this case, the union step makes writing the query
relatively easy and this is probably a good example of where the union step should be used. Namely,
when you want to combine multiple traversal results.
运行修改后的查询语句,查询结果以一种更易用的形式输出。
Running the amended query shows us the results in perhaps a more useful form.
浙公网安备 33010602011771号