图的查询常被称为是遍历,这也是我们实际上所做的事。我们从起始点开始一直遍历到终点。遍历包含了一个或者多个步骤(方法),这些步骤串联在一起。
A graph query is often referred to as a traversal as that is what we are in fact doing. We are
traversing the graph from a starting point to an ending point. Traversals consist of one or more
steps (essentially methods) that are chained together.
我个一起看一些简单的遍历,您会发现一些步骤常常会用到。最后,您会发现绝大多数的遍历都开始于g.V()或者g.E().有时候这些步骤中会用参数,我们稍后会了解这些步骤和参数。您可能还记得在每二节中,我们看了如何把航线图文件加载进来。我们用到了下面的指令来为我们加载的图创建图遍历源对象。
As we start to look at some simple traversals here are a few steps that you will see used a lot. Firstly,
you will notice that almost all traversals start with either a g.V() or a g.E(). Sometimes there will be
parameters specified along with those steps but we will get into that a little later. You may
remember from when we looked at how to load the air-routes graph in Section 2 we used the
following instruction to create a graph traversal source object for our loaded graph.
一旦有了图遍历源对象,我们就可以用它开始搜索图了。其中的V步骤用于返回顶点,E用于返回边,您也可以像在开始一样,在遍历的中间使用V步骤,我们会在后边说明这部分内容。V和E步骤都可以带有参数,这些参数用于指定我们感兴趣的顶点或者边。它们的使用会在“用ID工作”这一节中解释。
Once we have a graph traversal source object we can use it to start exploring the graph. The V step
returns vertices and the E step returns edges. You can also use a V step in the middle of a traversal
as well as at the start but we will examine those uses a little later. The V and E steps can also take
parameters indicating which set of vertices or edges we are interested in. That usage is explained in
the "Working with IDs" section.
如果能够帮助记忆的话,您可以把g.V()理解成“查看图中的所有的顶点”g.E()看成是“查看图中所有的边”。然后我们可以增加步骤从而缩小我们查询的范围。
If it helps with remembering you can think of g.V() as meaning "looking at all of
the vertices in the graph" and g.E() as meaning "looking at all of the edges in the
graph". We then add additional steps to narrow down our search criteria.
另一个我们需要介绍的步骤是“hasLabel”步骤。它们主要用于测试一个标签或者属性是不是有某一个值。通过本书,我们一起研究小精灵查询语言, 我们会遇到各种不同的小精灵的步骤,包括各种形式的:"has"步骤,这些内容足够我们开始了。
The other steps we need to introduce are the has and hasLabel steps. They can be used to test for a
certain label or property having a certain value. We will encounter a lot of different Gremlin steps
as we explore various Gremlin queries throughout the book, including many other forms of the has
step, but these few are enough to get us started.
您可以查看阿帕奇TinkerPop官方文档来获取本书中用到的图遍历的步骤的全部的细节。在本书中笔者并不是试图讲解每一个小精灵步骤和方法的用法。笔者只是尽力去提供有效的、易于理解的基础,有了这些基础,您可以在真实的图中编写各种小精灵查询。
You can refer to the official Apache TinkerPop documentation for full details on all of the graph
traversal steps that are used in this tutorial. With this tutorial I have not tried to teach every
possible usage of every Gremlin step and method, rather, I have tried to provide a good and
approachable foundation in writing many different types of Gremlin query using an interesting
and real-world graph.
最新版本的TInkerPop3 的文档您可以从如下地址找到。
The latest TinkerPop 3 documentation is always available at this URL:
http://tinkerpop.apache.org/docs/current/reference/
下面是一些我们开始学习的简单的对航线图遍历的语句。假定在执行每条语句之前,您已将航线图加载到了控制台。下面的查询会返回有airport 标签的所有顶点。
Below are some simple queries against the air-routes graph to get us started. It is assumed that the
air-routes graph has been loaded already per the instructions above. The query below will return
any vertices (nodes) that have the airport label.

这个查询会返回代表DWF机场的顶点
This query will return the vertex that represents the Dallas Fort Worth (DFW) airport.

下边的两个查询把之前的两个查询合并成一个。第一个查询只是把查询连在一起。第二个有一个“has”步骤,我们以前没有见过,它会把一个额外的标签做为它的第一个参数。
The next two queries combine the previous two into a single query. The first one just chains the
queries together. The second shows a form of the has step that we have not looked at before that
takes an additional label value as its first parameter
.
这是我们从上面的查询中得到的返回结果。要注意的是,小精灵控制台告知我们返回的顶点它的ID是8.
Here is what we get back from the query. Notice that this is the Gremlin Console’s way of telling us
we got back the Vertex with an ID of 8.
所以,我们实际上从这些查询中得到的是Tinker Pop顶点的数据结构。在本书的后面,我们会看到把这个值存储起来用于进一步处理的方式。 记住,尽管是在小精灵的控制台中,我们是在Groovy的环境中工作的,我们在这工作的所有一切,它的内核都是Java代码。所以我们可以用getClass方法从Java中获得对象。调用next,它会返回遍历结果到一个对象,我们可以进一步的处理这个对象。
So, what we actually got back from these queries was a TinkerPop Vertex data structure. Later in
this book we will look at ways to store that value into a variable for additional processing.
Remember that even though we are working with a Groovy environment while inside the Gremlin
Console, everything we are working with here, at its core, is Java code. So we can use the getClass
method from Java to introspect the object. Note the call to next which turns the result of the
traversal into an object we can work with further.
我们上边用的“next”步骤它是Tinerpop文档中描述的一系列步骤中的终止“terminal”步骤.在本书中,我们会看到多个这样的步骤。就像上边所说的,终止步骤实质上是停止了图的搜索,返回一个您可以在应用中使用的确切的对象。稍后,您也会看到在其它独立运行的程序中,我们用类似的“next”步骤和其它的一些相关步骤来查询图。我们甚至于可以在上面的查询的结尾调用getMethods()方法,这平就可以返回一个列表,列表中有TinkerVertex类的各种方法和它们的类型。
The next step that we used above is one of a series of steps that the Tinkerpop documentation
describes as terminal steps. We will see more of these terminal steps in use throughout this book. As
mentioned above, a terminal step essentially ends the graph traversal and returns a concrete object
that you can work with further in your application. You will see next and other related steps used in
this way when we start to look at using Gremlin from a standalone program a bit later on. We could
even add a call to getMethods() at the end of the query above to get back a list of all the methods
and their types supported by the TinkerVertex class.
浙公网安备 33010602011771号