在Tinkerpop3.2.4中增加了新的功能,它允许在where步骤后面使用by调节器。这使得编写特定类型的查询时比以前更加的容易。 相信截止现在,很多支持Tinkerpop的图存储已经支持这一新功能。但在开始设计查询之前确保图存储所支持的Tinkerpop的版本总是一个不错的主意。下面的查询从AUS机场出发,查找您可以到达的机场。where by 步骤在查询中用于过滤和AUS机场有相同跑道数量的机场。这种写法的好处是:在编写这个查询之前我们并不事先知道AUS有多少个跑道。
A new capability was added in the Tinkerpop 3.2.4 release that allows a where step to be followed
with a by modulator. This makes writing certain types of queries a lot easier than it was before.
Hopefully by now, this capability is supported in many TinkerPop enabled graph stores but it is
always a good idea to verify the version of TinkerPop supported before starting to design queries.
The query below starts at the Austin airport and finds all the airports that you can fly to from there.
A where by step is then used to filter the results to just those airports that have the same number of
runways that Austin has. What is really nice about this is that we do not have to know ahead of
time how many runways Austin itself has as that is handled for us by the query.

 

 

联合使用where和by 步骤,您可以轻松简单的写出功能强大的查询。
Combining the where and by steps allows you to write powerful queries in a nice
and simple way.
如果您在控制台运行,您会看到这些结果。注意所有的机场fjtb有两条跑道,这刚好就是AUS机场的跑道的数量(即相同的数量)。
If you were to run the query in the Gremlin Console, these are the results that you should see. Note
that all the airports returned have two runways. This is the same number of runways that the
Austin airport has.
 

 

联合使用where,by步骤,我们可以避免以更复杂的方式写查询,前边的查询的更复杂的一种写法如下所示:

The ability to combine where and by steps together allows us to avoid having to write the previous
query in more complicated ways such as the one shown below.

 

您可能注意到在上前的例子中,在where步骤中有两个参数。在这个例子中,第一个参数引用了查询早些时候定义的一个标签。看一下例子。我们查找AUS的机场顶点把它标记为a.然后要找所有从AUS出发可以到达的机场,标记为b,然后我们用一个where步骤来比较a 和b,只有比AUS小的一些机场返回了。

There is also a two parameter form of the where step that you may have noticed used above. In this
case the first parameter refers to a label defined earlier in the query. Take a look at the example
below. We find the vertex for the Austin (AUS) airport and label it 'a'. We then look at all the
airports you can fly to from there and label them 'b'. We then use a where step to compare 'a' and
'b'. Only airports with fewer runways than Austin should be returned.
运行这个查询,AUS有两条跑道,所有只有一条跑道的机场才能返回。
Austin has two runways so only airports with one runway are returned by this query when run.
 

 

也可以通过增加第二个by调节器来比较两个不同的属性。当属性有多个不同的键名,但是包含相同的值时,这是很有用的。下面的查询确实有些故意为之。您可以用更简单的方式得到同样的结果。但是它展示了两个by调节器的使用。一个机场顶点的国家属性与国家顶点的代码属性相比较。这个查询先找到城市city属性含有London的机场顶点。然后找到相连的国家顶点。where比较的是两个顶点的国家代码的值。最后的select步骤用于挑选我们想要的返回结果。

It is also possible to compare two different properties by adding a second by modulator. This is
useful when vertex properties have different key names but may contain the same values. The
query below is definitely contrived, you could achieve the same thing in a more simple way, but it
does demonstrate two by modulators being used. The country property of an airport vertex is
compared with the code property of a country vertex. The query first finds any airport vertex with a
city property containing the string London. Next any connecting country vertices (ones connected
by contains edges) are found. The where test compares the country code value of the two vertices.
Lastly a select is used to pick the results that we want to return.

 

 查询运行时,我们得到了所有城市名是London的机场,机场的地区代码region code 和国家代码country code.

When the query is run we get back all the airports with a city name of London along with their
region code and country code.
 

 

就像我所提到的,前边的查询只是一个例子。实际上,下面的查询语句没有用任何where步骤,也足以查找到。

As I mentioned the above query was used just as an example. In reality the following query that
does not use any where steps would have sufficed in this case.
 

 

设想一下,我们想写一个查询找到所有机场,它们和法国机场Nice有相同的区域代码region code. 我们假设现在我们不知道区域代码region code.是什么。所以我们不能写一个简单的查询去找到所有在那个地区的机场。这样我们就需要写一个查询先找到Nice的区域代码,然后用这个区域代码来找到其它在这个区域的机场。最后我们返回机场的代码airport code和城市名city name还有区域代码region code。可以利用where by 结构来写这个查询。看一下下面的查询和它生成的结果。

Let’s imagine that we want to write a query to find all airports that have the same region code as
the French airport of Nice. Let’s assume for now that we do not know what the region code is so we
cannot just write a simple query to find all airports in that region. So, instead we need to write a
query that will first find the region code for Nice and then use that region code to find any other
airports in the region. Finally we want to return the airport code along with the city name and the
region code. One way of writing this query is to take advantage of the where…by construct.
Take a look at the query below and the output it generates.
 

 

在样例文件夹中,您可以找到名为GraphRegion.java的程序,它表明了如何在Java程序中运行上面的查询。关于这个查询有一些很有趣的内容。首先因为我们比较了两个value步骤的结果,我们没有给by 步骤提供参数,因为我们不需要提供属性的键。第二,我们在查询中用了用二个V()步骤,用于查找所有有Nice有相同代码的机场。注意,这也意味着Nice也包含在了在结果里。最后,我们在local步骤里封着了部分查询让他以我们期待的方式准备输出结果,这样每个机场都有自己的一个列表。

In the sample programs folder you will find a program called GraphRegion.java
that shows how to perform the query shown above in a Java program.
There are several things about this query that are interesting. Firstly because we are comparing the
results of two values steps we do not provide a parameter to the by step as we do not need to
provide a property key. Secondly, we use a second V() step in the query to find all the airports that
have the same airport code as Nice. Note that this also means that Nice is included in the results.
Lastly we wrap the part of the query that prepares the output in a form we want in a local step so
that a separate list is created for each airport.
您也可以用其它方式写这个查询,比如使用match步骤,但是一旦您理解了上面的方式,就会体会到它是相当简单和功能强大的。
You could write this query other ways, perhaps using a match step but once you understand the
pattern used above it is both fairly simple and quite powerful.
posted on 2022-04-18 22:06  bokeyuannicheng0000  阅读(33)  评论(0)    收藏  举报