在本节中您会发出一些查询,它们检查了机场之间的距离。下面这个查询返回了从AUS出发的所有航线和它们的距离的一个列表。这些结果是按距离的升序排列的。
In this section you will find some more queries that examine distances between airports. The query
below returns a nice list of all the routes from Austin (AUS) along with their distances. The results
are sorted in ascending order by distance.
这是运行查询的结果。为了方便阅读,笔者将结果分成了四栏。
Here are the results of running the query. For ease of reading I again broke the results into four
columns.
这个查询找出来从DFW出发长度超过了4000英里的路线并返回了机场的代码和它们的距离。注意在这个查询中两个by调节器的使用,它们决定了从源顶点、边和目地的顶点返回哪些值。注意只指定了两个值却回来了三个值,这是因为如果值的数量多于by调节器的数量,by调节器的处理是回环方式的。(即第三个又回用到第一个指定的内容)
This query finds all routes from DFW that are longer than 4,000 miles and returns the airport codes
and the distances. Notice the use of two by modulators in this query to decide which values are
returned from the source vertex, the edge and the destination vertex respectively. Also note that
only two were specified but three values are returned. This works because by is processed in a
round robin fashion if there are more values than by modulators.
这是运行查询的结果。
Here are the results of running the query.
前边的结果没有顺序。我们可以修改查询让它包含一个order步骤,这样结果就会按距离的降序排列。
The previous results are not sorted in any way. We could modify the query to include an order step
so that the results are sorted in descending order by distance.
这是现在排序以后的结果。
Here are the, now sorted, results.
接下来这个查询是找出来所有长度超过了4000英里的路径,但是这次起点是伦敦LGW。注意使用了where来查询边的距离。has的形式相当简单但是笔都展示了where的使用是为了说明它是我们可以使用的另一种方式。注意这个查询用了三个by调节器,每个返回的值都来自顶点和边的不同的属性。
This next query also finds all routes longer than 4,000 miles but this time originating in London
Gatwick. Note also the use of where to query the edge distance. The has form is simpler but I show
where being used just to demonstrate an alternative way we could do it. Note that this query uses
three by modulators as each of the values returned is from a different property of the respective
vertices and edges.
这是运行查询的结果。
Here are the results from running the query.
下一个查询与前一个类似。我们找到了从DFW出发长过超过4500英尺的路径。但是,这个查询有一些不同值得注意。首先它用到了推荐的has技术来测试前一个查询中我们用where来测试的距离。这次我们只列出了距离、目的机场的代码并用一个sort对最终结果进行排序。我们也可以用select,as 而不是更简洁的path和by来说明达成同一结果的不同的方式。 path,by这个组合是最近才引入到Tinkerpop中的,笔者发现它在绝大部分时候是一种更妥贴的句法,但是这两种方式都可以奏效并各自有各自的好处。我们应当注意笔者在这使用sort只是为了显示一种不同的排序结果的方式。但绝大多数的情况,我们会使用order重写我们的查询.sort的使用会需要groovy闭包被提供。录在处理商用的图数据库时它会禁止闭包,这可能就行不通了。在本书中您会发现使用order的一些例子。在“对一些东西排序--order介绍”这一节中order步骤会被介绍。为了尽可能地保持纯小精灵的句法,推荐您避免闭包的使用。
This next query is similar to the previous ones. We look for any routes from DFW that are longer
than 4,500 miles. However, there are a few differences in this query worthy of note. First of all it
uses the preferred has technique again to test the distance whereas in the previous query we used
where. Also this time we just list the distance and the destination airport’s code and we sort the end
result using a sort. We also use select and as rather than the perhaps more succinct path and by to
show a different way of achieving effectively the same results. The path and by combination were
introduced more recently into TinkerPop and I find that to be a more convenient syntax to use most
of the time but both ways work and both have their benefits. We should also note that I show sort
being used here just to show a different way of ordering results, but in most cases we can re-write
our query to use order. This use of sort requires a Groovy closure to be provided. This may not
work when working with commercial graph databases that disallow closures. You will find several
examples of order being used throughout this book. The order step is introduced in the "Sorting
things - introducing order" section. As much as possible staying with the pure Gremlin syntax and
avoiding closures is recommended.
总的来说,笔者发现 使用path会比select,as更清晰一些。然而就如在“警告:path步骤会是内存和CPU密集的”这一节讨论过的一样,path消耗内存的问题存在,您可能会运行一些复杂的查询。对于如果写您的小精灵查询,有一些选项(有多种方式)总是好的。这是这个查询产生的输出。
In general, I find using path rather than select and as to be cleaner. However, as discussed in the "A
warning that path finding can be memory and CPU intensive" section, there are issues with path
consuming memory that you will likely run into with more complex queries so it is always good to
have some options as to how you write your Gremlin queries. Here is the output produced by this
query.
出于完整性,这是用order步骤而不是调用sort做为查询最后处理步骤,重写的一个查询。 通常更推荐这种方式来得到排序的结果。
For the sake of completeness, here is the query re-written to use an order step rather than a call to
sort as a post processing step at the end of the query. In general this is the recommended way of
achieving sorted results.
注意使用了select步骤在被集中起来排序前。这是运行修改后的查询的输出。
Note that the results are ordered before being collected using the select step. Here is the output
from running the modified query
查询也可写成order步骤跟在select步骤之后,如下这样。
The query can also be written with the order step coming after the select step. As follows: