您在使用小精灵控制台的时候,笔者已经提到过,一些事情控制台为您悄悄处理了。当你写独立运行的Java应用程序,您就需要自己来处理这些事情。关键是笔者发现人们从控制台转到Java应用程序时会困惑:哪些类和枚举类型控制台已经在后台导入了,如何从Java去实现这些能力。您应当把TinkerPop的API添加为您的网页书签,这样您就可以找到的所有这些类和枚举类的详细说明了。最新的javadoc在这 http://tinkerpop.apache.org/javadocs/current/full/
When you are using the Gremlin Console, as I have already mentioned, a few things are done for
you that you need to take care of yourself when writing standalone Java code. A key area that I
have found that people making the jump from the console to Java find confusing is figuring out
which Classes and Enums have been statically imported "behind the scenes" and how to access
those same capabilities from Java. You should also bookmark the TinkerPop javadoc pages as you
can find more detail on all of the classes and enums covered below there. The latest javadoc is
always available at http://tinkerpop.apache.org/javadocs/current/full/
有一个样例程序名为TestImports.java 它在样例代码文件夹,这个位置,它说明了这些结构被用到了。
There is a sample program called TestImports.java in the sample-code folder
located at https://github.com/krlawrence/graph/tree/master/sample-code that
demonstrates these constructs being used.
下面的表格左边一列说明了一些小精灵中常见的关键字。第二列是您可以在Java程序中引用的相同的关键字。第三列是它们用在java程序中的例子。您可以静态导入这些类到您的代码中。笔者更喜欢使用明确的前缀,但是多数情况下这是一个个人喜好的问题。有一个所有可用的前缀的表格在“测试值和值的范围”这一节中。对于特别的双下划线前缀,笔者只举了几个例子。总的来说您可以在您的查询没有前边的对象可以用点号串联的时候使用这个前缀。
The tables below show some commonly used Gremlin keywords in the left column. The second
column shows how you would reference those same keywords explicitly from a Java program. The
third column shows an example of the context in which they might be used in a Java program. You
may chose to statically import some of these classes into your code. I prefer to use the explicit
prefix but that is a matter of personal preference in many cases. There is a table of all the available
predicates in the "Testing values and ranges of values" section. For the special ".__" class I have just
shown a few examples. In general you use this prefix when you have nothing prior in the query
that you can "dot chain" to.
在把一个属性加到顶点上时,如果您需要说明属性值怎么被对待时,您可以用一个已定义为Cardinality枚举类的一部分的一个关键字.这个枚举类是顶点属性VertexProperty接口的一部分。
If you need to specify how a property value should be treated when added to a vertex you can use
one of the keywords defined as part of the Cardinality enum which is part of the VertexProperty
interface.
排序的方向需要说明时,local范围可以用来做为sort order的一个参数。定义在Scope中的常量和和Order枚举类就会被用到。
When local scope needs to be specified as a parameter of sort order direction needs to be specified
the statics defined in the Scope and Order Enums can be used.
Order.incr 和 Order.decr 这两个枚举值在TinkerPop3.3.4中已淘汰了,取而代之的是Order.asc 和 Order.desc,这两个关键字和其它的查询语言中的常用的关键字更一致。从TinkerPop3.5.0开始,incr和decr将一起从小精灵查询语言中移除了。
The Order.incr and Order.decr enumerations were deprecated in the TinkerPop
3.3.4 release in favor of Order.asc and Order.desc to bring the keywords more into
line with other commonly used query languages. As of TinkerPop 3.5.0, incr and
decr were removed from the Gremlin query language altogether.
如果您需要访问map数据结构中的键或值,您可以用到定义在Column中定义的常量。
If you need to access the keys or values from a map data structure you can use the statics defined in
the Column enum.
在从一个valueMap访问id和标签值时,您可以用到定义在T枚举类中的常量。同样的哪果您要访问属性集合的键和值时您可以用到它们。
When accessing the id and label values from a valueMap you need to use the statics defined in the T
Enum. The same is true if you want to access the keys and values from a set of properties.
当没有前边的步骤可以进行点号连接时,我们可以就把双下划线类做为我们的前缀。如果您看了这个类的javadoc,您就会看到它的静态方法的定义,我们可以用来调用小精灵的功能,在这种情况下,像下面这样。
When there is no previous step to "dot chain" to then we can use the ".__" class as our prefix. If you
look at the javadoc for the class you will see it defines static methods that we can use to call
Gremlin functionality in cases like the ones shown below.
无论何时我们需要说明执行一个测试,我们可以到定义在P类当中的静态的方法。定义的静态方法并没有在下面全列示出来。
Whenever we need to use a predicate to perform a test, we can use the static methods defined in
the P class. Not all the methods defined are shown below.
阿帕奇TinkerPop3.4引入了一些新的文本的断言,和一个新的TextP类。
The Apache TinkerPop release 3.4 introduced some new text predicates and a new TextP class.
如果遍历路径有多个值与同一个标签相关联,比如标签a,这时您就可以用first,last, all, mixed这些定义在Pop枚举类中的常量。就像它们的名称说明的,first会返回集合中的第一个,说明last,就会返回最后一个记录,all会返回集合中的所有的记录。说明为mixed将会返回一个列表,如果集合中有一个以上记录。否则就会把对象返回来。
If a traversal path has multiple values associated with a single label, such as "x" then you can use
the first, last , all and mixed statics that are defined as part of the Pop Enum. As the name suggest,
first returns the first item in a collection. Specifying last returns the last item and all returns all of
the items in a collection. Specifying mixed will return a List if the collection has more than one item.
Otherwise an Object will be returned.
在进行sack相关的处理时,类似sum,assign 这样的运算符会用到,它们定义在了Operator枚举类中。
When working with a sack the operators like sum and assign are defined in the Operator Enum.
Table 15. Operators
Direction这个枚举类定义了常量,可以用到边的方向上。
The Direction Enum defines constants that are used in association with edge direction.
另一个有用的提示:这个是在小精灵用户邮件列表中大家分享的。您可以让小精灵控制台告诉您它为您在幕后悄悄地导入的那些内容。可以用:show imports这个命令。可能返回的内容如下所示。笔者包含了一个版本的检查信息在输出中,这样将来版本变化了,您就可以看到笔者查询时所用的小精灵的版本了。
Another useful tip, that was shared on the Gremlin Users mailing list, is that you can ask the
Gremlin Console to show you a list of everything that has been imported on your behalf "behind the
scenes" using the command :show imports. What might typically be returned is shown below. I
included a version check in the output so in case this changes in the future you can see which
version of Gremlin I queried.
这是我们开始时,小精灵的控制台在幕后为我们设置的已导入的列表。特别注意那些静态导入的,它们含了那些我们前边谈到的定义。
Here is the list of imports that the Gremlin Console has setup for us quietly behind the scenes when
we started it. Take particular note of the ones that are static imports as those are the ones that
contain the definitions we discussed above.
![]()
![]()
前边提到过,在小精灵控制台,您可以总是使用getClass方法或.class 很多情况下,找出来一些东西在哪定义的。在本节前边我们看到的那些例子,大量的关键字比如value,id,local定义成了枚举类型,您可以直接对它们使用getClass。下面是这样的一些例子。
As discussed earlier, you can always use the getClass method or simply .class while using the
Gremlin Console to, in many cases, find out where something is defined. As we saw in the examples
earlier in this section, lot of the keywords such as values, id and local are defined as Enums so you
can use getClass on them directly. A few examples are shown below.
如果您把这些输出与上边的表格对比,您可以看到我们能去验证:例如,label和key定义在T 枚举类中。我们也可以看到keys, values定义在Column枚举类中。最后我们可以看到local,如我们所料,定义在了Scope枚举类中。
If you compare this output to the tables above you can see that we have been able to verify that, for
example, that label and key are defined in the T Enum. We can also see that keys and values are
indeed defined in the Column Enum. Lastly, we can see that local is, as we expected, defined in the
Scope Enum.