在本书中出现了很多个使用groupCount和group步骤的例子。然而有一些内容到目前为止笔者还没有涉及到。也许很明显,也许还不是,那就是您可以把一个group步骤嵌套在另一个group 步骤中。这可能是因为事实上by调节器可以用来准确地告诉group步骤您想要分组的可以发生在任意的遍历中。看下面这个例子。希望缩进可以让它变得更易读。我们从找五个机场开始分组,每组的键是机场的代dcgf。这是由第一个by调节器指定的;值的部分由第二个by调节器说明,我们开始了第二个遍历。这个遍历是从您一开始找到的五个机场中的一个出发,可以到达的五个地方。另一个group步骤用来给这些机场分组,这次是按照每个机场有的出边的总数分组。
Many examples of both the groupCount and group steps have already been presented in this book.
However, something I have not touched on so far, which by now may be obvious but perhaps not, is
that you can nest one group step inside another one. This is made possible by the fact that the by
modulators that are used to tell the group step precisely what you want grouped can take arbitrary
traversals. Take a look at the example below. Hopefully the indentation makes it easier to read. We
begin by simply finding five airports and starting a group. The key for each group will be one of the
codes for each of these airports. This is specified by the first by modulator. For the value part of
each group, specified by the second by modulator, we start a second traversal. That traversal begins
by finding five places you can fly to from each of the five airports that we initially found. Then
another group step is used to group each of those airports by the number of total outgoing routes
they have.
运行这个查询,返回如下。如您所料,外边的分组的键代表了最开始找到的那五个机场,里边的分组它的键是五个目的地机场,它的值是它们的出边的数量。为了方便阅读,笔者优化打印了输出的信息
When the query is run here is what we get back. As expected the outer group has keys that
represent the first five airports that were found. The inner group has the five airport destinations
as the keys and their total outgoing route counts as their values. I pretty printed the output a bit to
make it easier to read.
我们创建的分组实际上是一个map,机场的代码是它的键,额外的分组是它的值。因此每个组由键值对组成,我们可以用一个select步骤来只抽取一些结果。
So what we have created is a group, essentially a map, that has airport codes as the key and an
additional group as the values. Therefore, given each group is made up of key value pairs, we can
use a select step to only pick a few of the results.
这次只有被选的结果返回啦。
This time only the selected results are returned
如果您想从里边的组里选择一些结果,您可以用另外一个select步骤完成它。
If you want to select results from one of the inner groups you can do that as well using an
additional select step.
这将先择出来键是ANC的结果然后再从组里选出键是IAH和LAX的结果。
This will first select the result with a key of ANC and then from that group select the results for the
keys of IAH and LAX only.
5.1.17. 分析欧洲和美国之间的航线 Analysis of routes between Europe and the USA
下面的一些查询向您说明了您如何用像航线图这样的图来进行一些特定的行业分析。下面的查询分析了美欧机场的分布和可能性。首先让我们找出来欧洲的机场和美国的机场之间有多少航线。
The next few queries show how you can use a graph like air-routes to perform analysis on a
particular industry segment. The following queries analyze the distribution and availability of
routes between airports across Europe and airports in the United States. First of all let’s just find
out how many total routes there are between airports anywhere in Europe and airports in the USA.
现在我们知道了有345条不同的航线。记住,尽管航线图没有跟踪这些航线的运行的数字。这个图只是存储了数据:至少有航空公司运行这些不同的往返的航线。让我们再进一步,这345条线路中有多少是个美国的机场可以从欧洲出发到达的。
So we now know that there are 345 different routes. Remember though that the air-routes graph
does not track the number of airlines that operate any of these routes. The graph just stores the
data that at least one airline operates each of these unique route pairs. Let’s dig a bit deeper into
the 345 and find out how many US airports have flights that arrive from Europe.
所以现在我们看到这345条线路中,从欧洲出发,达到了美国的38个机场之一。 我们可以再进一步,看看这38个机场的践线的分布。
So we can now see that the 345 routes from European airports arrive at one of 38 airports in the
United States. We can dig a bit deeper and look at the distribution of these routes across the 38
airports.
JFK这个在美国纽约的机场看起来有最多的从欧洲到达的线路,排在第二的是EWR。
John F. Kennedy airport (JFK) in New York appears to have the most routes from Europe with
Newark (EWR) having the second most.
现在让我们重复这样过程看看目的地是欧洲的。首先我们可以计算有多少个欧洲的机场可以飞抵美国。
Now let’s repeat the process but looking at the European end of the routes. First of all, we can
calculate how many European airports have flights to the United States.
就像我们对美国的机场所做的一样,我们可以找出来欧洲的机场的路线的分布。
Just as we did for the airports in the US we can figure out the distribution of routes for the
European airports.
看起来 LHR飞往美国的最多,第二是FRA。
It appears that London Heathrow (LHR) offers the most US destinations and Frankfurt (FRA) the
second most.
最后,我们可以找出来飞行的路线的列表。举个例子,笔者决定只返回345条线路中的10个。注意如何让path步骤返回遍历中的各个部分,并包含大洲代码EU。我们可以加一个from调节器来移除结果的一部分, 就像在前边的“我访问过什么顶点和边?”这一节中说明的一样。
Lastly, we can find out what the list of routes flown is. For this example I decided to just return 10
of the 345 routes. Note how the path step returns all parts of the traversal including the continent
code EU. We could remove that part of the result by adding a from modulator as shown earlier in
the "What vertices and edges did I visit? - Introducing path" section.
前10个返回的路线来自Warsaw, Belgrade and Istanbul.
The first 10 results returned feature routes from Warsaw, Belgrade and Istanbul.
浙公网安备 33010602011771号