这一节中的例子是基于我们以前所学的所有的话题而设计的。下面的查询是找到从US-HI任一个机场出发可以到达的城市。
The examples in this section build upon the topics that we have covered so far. The query below
finds cities that can be flown to from any airport in the Hawaiian islands.
如果运行查询,我们会得到下面这样的结果。只展示了全部结果的一部分。
If we run the query we would get back results similar to those below. Only a few of the full result
set are shown.
下面的查询是使用代码code是EU的大洲顶点continent vertex做为起始点找到所有在欧洲的机场。结果按升序排列并把它们封装到一个列表中。
The query below looks for airports in Europe using the continent vertex with a code of EU as the
starting point. The results are sorted in ascending order and folded into a list.
这是我们运行查询得到的结果。
Here is what we get back from running the query.
下一个查询是找到有6个或以上跑道的机场的两种方式。
The next queries show two ways of finding airports with 6 or more runways.
接下来,我们看一下两种方式找到从南美的一个机场出发到达Miami的航班。第一个查询用了select,这个是我们在Tinkerpop2时代可以使用的一种方法,第二个查询,对于笔者来说更清晰易懂,使用了Tinker pop3中引入的path和by步骤的组合。
Next, let’s look at two ways of finding flights from airports in South America to Miami. The first
query uses select which is what we would have had to do in the TinkerPop 2 days. The second
query, which feels cleaner to me, uses the path and by step combination introduced in TinkerPop 3.
这个查询找到了AUS和DFW之间的边,返回了边的距离属性,这样我们就可以知道旅途有多远。
This query finds the edge that connects Austin (AUS) with Dallas Ft. Worth (DFW) and returns the
dist property of that edge so we know how far that journey is.
一种替代方案,我们返回路径包含机场代码和距离。注意我们需要使用outE,inV而不只是out, 因为我们需要把边做为我们路径的一部分,这样我们就可以用一个by步骤得到它的dist距离属性了。
As an alternative approach, we could return a path that would include both airport codes and the
distance. Notice how we need to use outE and inV rather than just out as we still need the edge to be
part of our path so that we can get its dist property using a by step.
如果我们想要找到从BNE到AUS只中转一次的路线。这个查询就可以做到。
If we wanted to find out if there are any ways to get from Brisbane to Austin with only one stop,
this query will do nicely!
同样的事可以用另一种方式实现,这次,对笔者而言更加的精准。这个查询的唯一的优点是,如果您只想在道中间转机的机场的名称,它就是您得到的全部信息。
This is another way of doing the same thing but, once again, to me using path feels more concise.
The only advantage of this query is that if all you want is the name of any intermediate airports
then that’s all you get!
您会发现您在一个图上工作时,最常做的事情就是统计图中的一些东西。接下来这个查询是找出图中有小于5条出边的机场,统计它们的总数。这个查询统计了有少于5条带有route标签的出边的机场的数量。只提供这么少的目的地的机场数,这个数字出奇的高。
A common thing you will find yourself doing when working with a graph is counting things. This
next query looks for all the airports that have fewer than five outgoing routes and counts them. It
does this by counting the number of airports that have fewer than five outgoing edges with a route
label. There are a surprisingly high number of airports that offer this small number of destinations.
类似的方式,这个查询找到有超过200个出边的机场。第二个查询表明,在很多场景下,where和filter过滤是同义词
In a similar vein this query finds the airports with more than 200 outgoing routes. The second
query shows that where is a synonym for filter in many cases.
这有两个查询找到满足条件的东西。第一个找到距离是100英里的路径并返回它的始点和终点机场的代码。这第一个查询用了as和select步骤,而第二个用了path步骤,并把distance距离包括在了结果中。
Here are two more queries that look for things that meet a specific criteria. The first finds routes
where the distance is exactly 100 miles and returns the source and destination airport codes. The
first query uses as and select while the second one uses path and includes the distance in the result.
和前一个查询类似,使用path,并显示了距离和机场代码。
Similar to the prior query but using path and displaying the distance as well as the airport codes.
这个查询是找出海拔在10000英尺以上的机场。两种方式实现相同的结果,如下所示,第一个查询用了valueMap,第二个用了project步骤。
This query looks for any airports that have an elevation above 10,000 feet. Two ways of achieving
the more or less the same result are shown. The first uses valueMap and the second uses a project
step instead.
如果运行使用project的查询,这是我们得到的返回结果。
If we ran the query that uses the project step here is what we should get back.
下面的查询找到AUS和SYD之间中转一次的路径。by步骤提供了一种清晰的方法和path 步骤一起使用,完成这个查询。
The next query finds any routes between Austin and Sydney that only require one stop.The by step
offers a clean way of doing this query by combining it with path.
下面的三个查询做了同一件事,它们找到从AF任一机场出发,到达US任一机场的航班。这些查询很有意思,因为在图中大洲信息在图中是以边的形式表示的。这些边连接一个机场顶点和一个大洲顶点。这大概就是小精灵和SQL的连接语句最相似的一个例子。
The following three queries all achieve the same result. They find flights from any airport in Africa
to any airport in the United States. These queries are intresting as the continent information is
represented in the graph as edges connecting an airport vertex with a continent vertex. This is
about as close as Gremlin gets to a SQL join statement!
第一个查询从找到机场开始,第二个从代表AF的顶点开始,第三个使用where展示了实现同样结果的一种替代方法。
The first query starts by looking at airports the second starts from the vertex that represents Africa.
The third query uses where to show an alternate way of achieving the same result.
运行查询,我们会得到类似这样的结果。为了节省空间,笔者把它们放到了两列。
If we run the query we should get results that look like this. I have laid them out in two columns to
save space.
下面的查询展示了如何使用TinkerPop3中引用了project步骤,把它和order,select步骤一起使用来生成一个有序表,表中是你可以从AUS出发到达的机场,和机场的跑道数。limit步骤用来限定只返回结果中的前10个。您会在本书中发现一些例子:在这样的集合步骤中使用变量。
The query below shows how to use the project step that was introduced in TinkerPop 3, along with
order and select to produce a sorted table of airports you can fly to from AUSTIN along with their
runway counts. The limit step is used to only return the top ten results. You will find several
examples elsewhere in this book that use variations of this collection of steps.
这是我们运行查询的结果。
Here are the results we get from running the query.