我们在之前已经说过,小精灵的控制台是基于Groovy控制台的。Groovy本身是使用Java语言编写的。这就是说,所有就可以在编写Groovy和Java程序时可以使用的方法和类,也可以在小精灵控制台上使用。您可以混合使用额外的来自Groovy和Java类的特性,与Tinkerpop3的类所提供的特性一起使用。这一功能使得小精灵更加的强大。您也可以在小精灵服务器或者其它支持TinkerPop的图存储中利用这些特性,提示:一些特性可能会被服务器视为是安全危胁而阻止,或者只是因为它们并不支持而被禁用。
As we have already discussed, the Gremlin console builds upon the Groovy console, and Groovy
itself is coded in Java. This means that all of the classes and methods that you would expect to have
available while writing Groovy or Java programs are also available to you as you work with the
Gremlin Console. You can intermix additional features from Groovy and Java classes along with the
features provided by the TinkerPop 3 classes as needed. This capability makes Gremlin additionally
powerful. You can also take advantage of these features when working with Gremlin Server and
with other TinkerPop enabled graph services with the caveat that some features may be blocked if
viewed as a potential security risk to the server or simply because they are not supported.
事实上,我们已经呈瑞的每个小精灵查询都是好的Groovy代码。我们已经展示了几个把值存入变量,并使用Groovy结构循环,做为小精灵查询的一个或者多个部分。
Every Gremlin query we have demonstrated so far is also, in reality, valid Groovy. We have already
shown examples of storing values into variables and looping using Groovy constructs as part of a
single or multi part Gremlin query.
在这一小节中,我们将进一步的使用Groovy句法,定义一些方法,它们仍然可以在小精灵的控制台内运行。举个简单的例子,我们定义一个方法,它将告诉我们两个机场距离有多远。然后调用这个方法。
In this section we are going to go one step further and actually define some methods, using Groovy
syntax, that can be run while still inside the Gremlin Console. By way of a simple example, let’s
define a method that will tell us how far apart two airports are and then invoke it.
接下来这个例子展示了如何定义一个长一点的方法,打印输出顶点的度的信息,以一种优雅的方便人们阅读的方式输出。
This next example shows how to define a slightly longer method that prints out information about
the degree of a vertex in a nice, human readable, form.
这个例子呈现了我们如何查询一张图,得到一个值的列表,然后用循环来显示它们。注意这次,我们把查询的结果放在了变量x中,然后调用toList这样就可以保证返回的结果是一个列表(数组)。
Here is an example that shows how we can query the graph, get back a list of values and then use a
for loop to display them. Notice this time how we initially store the results of the query into the
variable x. The call to toList ensures that x will contain a list (array) of the returned values.
有时,如您在上面看到的,有必要调用next来得到您所期待的返回的结果到指定的变量中。
Sometimes (as you have seen above) it is necessary to make a call to next to get the result you
expect returned to your variable.
这是一个例子:小精灵查询在一个循环的内部。
Here is another example that makes a Gremlin query inside a for loop.
这个例子返回了顶点的哈希表,顶点的标签是哈希表的键,而代码属性是哈希表的值。然后用标签名去访问返回来的哈希结果。
This example returns a hash of vertices, with vertex labels as the keys and the code property as the
values. It then uses the label names to access the returned hash.
这是另一个例子,这次我们定义一个方法 ,把图的遍历对象做为一个参数,另一个参数是机场的code,然后用这些参数来进行一个简单的小精灵查询,找到所有您可以从那个机场出发到达的所有的地方。然后用一个简单的for循环打印输出结果到一个表中。注意在输出的部分用到了next()方法。为了得到我们要找的实际的值这是必须的。如果我们不把next包含在代码中,我们将只得到迭代对象本身,而不是实际值。
Here is another example. This time we define a method that takes as input a traversal object and
the code for an airport. It then uses those parameters to run a simple Gremlin query to retrieve all
of the places that you can fly to from that airport. It then uses a simple for loop to print the results
in a table. Note the use of next as part of the println. This is needed in order to get the actual values
that we are looking for. If we did not include the calls to next we would actually get back the
iterator object itself and not the actual values.
这个例子创建了一个所有机场的哈希表,其中的键是机场的IATA代码。我们可以用IATA代码为访问map中的信息,查询这些机场的信息。记住:结尾的;[] 这部分内容是为了阻止控制台显示我们不需要的输出。
This example creates a hash map of all the airports, using their IATA code as the key. We can then
access the map using the IATA code to query information about those airports. Remember that the
;[] at the end of the query just stops the console from displaying unwanted output.
另一种使用变量的方式是建立变量然后使用fill步骤把查询的结果放到变量中。下面这个例子创建了一个空列表称为german,后边的查询语句找到了所有位置在德国的机场,然后使用fill步骤把它们放到变量中。
Another useful way to work with variables is to establish the variable and then use the fill step to
place the results of a query into it. The example below creates an empty list called german. The
query then finds all the vertices for airports located in Germany and uses the fill step to place them
into the variable.
如果您需要,我们可以使用我们的列表。记住如果我们是在控制台上运行,我们不需要强制的进行列表的迭代,像您在写一个单独运行的Groovy应用程序那样进行列表的迭代。
We can then use our list as you would expect. Remember that as we are running inside the Gremlin
console we do not have to explicitly iterate through the list as you would if you were writing a
standalone Groovy application.
在本书的最后,“在Groovy应用程序中使用廷克图”这一节中, 我们会探讨在小精灵控制台之,使用TinkerPop API编写独立运行的Groovy代码,外做为单独的应用,执行小精灵查询。
Towards the end of the book, in the "Working with TinkerGraph from a Groovy application" section,
we will explore writing some standalone Groovy code that can use the TinkerPop API and issue
Gremlin queries while running outside of the Gremlin Console as a standalone application.