在前一节中讨论过,当您使用小精灵断言如eq,neq时,在java程序中您需要用到前缀P,它会引用到同名的Tinker pop类,其中有一系列静态的方法,代表了已定义好的小精灵的断言
As discussed in the previous section, when you use a Gremlin predicate such as eq or neq from a
Java program you need to prefix it with a "P." which is a reference to the TinkerPop class of the
same name where a set of static methods, representing the Gremlin predicates, are defined.
您可以找到名称是GraphRegion.java的样例程序,它包含了本节中用到的代码,在样例文件目录,这个位置。
You will find a sample program called GraphRegion.java, which contains the code
used in this section, in the sample files directory located at https://github.com/
krlawrence/graph/tree/master/sample-code.
看下面的代码。定义了一个名为 findByRegion的方法,它的传入参数是字符串代表了三个字母的机场IATA代码。这个方法会用小精灵查询来找出来指定的机场在哪个地理区域,然后返回所有也在那个其域的所有的机场。注意在where步骤中使用了P.eq .这是另一个例子,因我们不是在小精灵控制台中运行查询,所以我们就要更明确的指定引用的对象。
Take a look at the code below. A method called findByRegion is defined that takes a String
representing a three character airport IATA code as input. The method then uses a Gremlin query to
figure out which geographical region the specified airport is in and then returns all airports also in
that region. Note the use of P.eq as part of the where step. This is another case of where, because we
are not running inside the Gremlin console, we have to more precisely specify things.
 

 

 调用findByRegion方法并传递参数DEN,它代表的是 Denver Colorado机场,下面的输出是将会返回的。

If we were to call the findByRegion method, passing in a parameter of DEN, representing the airport
in Denver Colorado, the following output should be returned.
 

 

 

6.1.7.查看一个查询是否返回了结果。 Checking to see if a query returned a result
为了避开那些烦人的空指针异常,在引用它们之前,知道一个查询是否返回了结果,这是很重要的。先别担心Java,单纯从小精灵查询语句的视角来考虑下面的查询语句。
It is often important to know if a query returned a result before trying to reference it to avoid those
pesky Java Null Pointer Exceptions. Without worrying about Java for a second consider the query
below purely from a Gremlin point of view.
 

 

 这个查询从AUS机场开始,然后找到与SYD关连的出边,返回边的距离。问题是,AUS和SYD之间没有直接的路线。恩些没有边可以找到距离。换句话说,下面的查询没有结果返回。现在在小精灵控制台这不是一个问题,我们只是什么都没返回,查询就结束了。然而看下面的代码,这是把查询移入到Java代码中的第一次尝试。

The query finds the Austin (AUS) airport and then looks for an outgoing edge connecting Austin
with Sydney (SYD) and then returns the distance value from that edge. The problem here is that
there is no direct route between Austin and Sydney and therefore no edge to retrieve the distance
from. In other words, this query returns no result. Now, within the Gremlin Console this is not a
problem as we just get nothing back and life goes on. However, take a look at the code below which
is a first attempt at moving the query into Java code.
 

 

表面看起来,它是好的。然而我们执行这些代码,将会遇到空指针的异常。因为当我们调用了next方法,没有结果可处理,因为AUS和SYD之间没有边,更没有距离值可处理。 

On the surface, this looks fine. However, were we to execute this code we would get a Null Pointer
Exception as when we try to call next there is no result to process as there is no edge between
Austin and Sydney and hence no distance value to process.
您可以找到名为GraphSearch2.java的样例程序,它包含了本节用到的代码,在样例文件目录中
https://github.com/krlawrence/graph/tree/master/sample-code.
You will find a sample program called GraphSearch2.java, which contains the code
used in this section, in the sample files directory located at https://github.com/
krlawrence/graph/tree/master/sample-code.
所以我们需要一种方式来查看我们是否得到了一个合法的结果。这种方式之一就是把结果存在一个列表中。最差的情况,如果没有结果被找到,我们会得到一个空列表返回。所以我们可以像下面这样重写查询。
So we need a way to check to see if we go a valid result. One such way is to store the result of the
query into a list. Then, worst case, if no results are found, we will get an empty list back. So, we can
rewrite the query as follows.
 

 

 结果在列表中, 我们可以安全地查看我们是否得到了结果。

Now that the result is in a list we can safely check to see if we got any results.
 

 

如果我们需要一个“纯小精灵的”方案,不用list或者进行一些后处理,另一种我们可以用的方法就是使用coalesce步骤,返回一个特别的常量值,在这个例子中就是-1,它说明了没有结果被找到。 

If we wanted a "pure Gremlin" solution, without using a List and without doing some post
processing, one way we could do it is to use the coalesce step and return a special constant value, in
this case minus one, to indicate that there were no results found.
 

 

 如果路径存在,距离就会被找到并返回,否则值-1被返回。这实际上和toList的例子使用了相同的理念,除了在这个情况我们在查询中用的是fold步骤来生成list。 unfold会返回强果如果列表不是空的,否则返回常量,coalesce会把第一个值返回做为结果。

If the route exists the distance will be found and returned, otherwise a value of "-1" will be
returned. This is really using the same concept as the toList example except in this case we generate
the list using the fold step within the query itself. The unfold will return a result if the list is not null,
otherwise the constant value will be returned as coalesce returns the first to yield a result.
当然佻可以找到多种方式来解决这个问题,但是使用list可以提供一种相当容易的解决方案。
There are of course many other ways that you might come up with to solve this problem but using
lists often provides a fairly easy to use solution.
6.1.8. 用Java应用程序创建新的图Creating a new graph from a Java application
下面的代码创建了一个新的空廷克图实例,创建了一个图遍历源对象,然后用遍历来创建小图。
The code below creates a new (empty) TinkerGraph instance, creates a graph traversal source
object and then uses a traversal to create a small graph.
这个例子的全部源码可以在CreateGraph.java文件中找到,它们在这https://github.com/krlawrence/graph/tree/master/sample-code
The full source code for this sample can be found in the file CreateGraph.java
located at https://github.com/krlawrence/graph/tree/master/sample-code
注意调用了iterate()在遍历的最后。做为独立的应用程序运行,这么做是必须的。 这是另一个小精灵控制台为您在幕后做的处理,您可能还不知道。当不在控制台进行处理时我们需要记得自己来处理。
Note the call to iterate() at the end of the traversal. When running as a standalone application this is
necessary. This is another of those little things that the Gremlin Console does for you without you
realizing it that we have to remember to do ourselves when not running inside the console.
 

 

 已经创建了新图,我们可以运行一些查询来确保它是正确的。首先,我们查看创建的顶点,查看它们的ID。和前一个例子一样,调用了带参数true的valueMap,返回我们需要的。我们从这些代码中得到的是一个map的列表。每个map包含的键有,机场代码、顶点ID,顶点标签。

Having created a new graph we can run some queries to make sure it looks correct. Firstly, let’s
check that the vertices were created and look at the IDs that were allocated to them. As with prior
examples, a call to valueMap with a parameter of true will return what we need. What we will get
back from this code is a list of maps, with each map containing keys for the airport code, the vertex
ID and the vertex label.
 

 

 得到了返回的map列表,我们可以处理它们。注意为了从列表中得到id和label值,笔者在键名前加了前缀T. 这是历为大多数属性键是字符串, ID和label是特殊情况。如果您看了Tinker pop的文档您就会发现T是Java枚举类,包含了T.id, T.label, T.value和T.key.的定义。在java中使用小精灵时,重要的是要记得我们需要在这种情下用到T.前缀,而如果是使用小精灵控制台我们就不需要这么做。

Having got our list of maps back we can process them. Note that to get the id and label values from
the map I had to prefix the key name with a "T.". This is because while most of property keys are
Strings, IDs and labels are a special case. If you look at the TinkerPop documentation you will see
that T is a Java Enum that contains definitions for T.id, T.label, T.value and T.key. When working
with Gremlin in Java it is important to remember that we need to use the "T." prefix in cases where
when using the Gremlin Console we would not have to.
 

 

 图创建期间一切顺利, 我们会得到一个列表,像下面这样,它向我们显示了每个顶点的ID。

If all has gone well during graph creation, we should get back a list like the one below that shows us
the ID that has been given to each vertex.
 

 

最后,通过显示新的图中的所有的路径,我们可以验证我们的边也正确地创建了。 

Finally, let’s check that the edges were created correctly by displaying all of the paths between
vertices in our new graph.

 

再次,如果一切都如期望的工作,这是我们可能会得到的结果。 

Once again, if everything has worked as expected, here is what we should get back.
 

 

 

6.1.9. 在Java应用程序中保存一幅图 Saving a graph from a Java application
已经创建了新图,我们可能想保存它。下面的代码显示了如何把图保存成GraphSON 或GraphML (XML)格式。这是另一个实例,我们得做更多的工作,因为我们是在运行一个独立运行的程序,而不是在小精灵控制台内处理它。我们得尝度捕获在保存数据时可能出现的异常。这部分代码也包含在了 CreateGraph.java样例中。
Having created our new graph, we may want to save it. The code below shows how to save the
graph as either GraphSON (TinkerPop’s JSON format) or as GraphML (XML). This is another
instance where we have to do a bit more work as we are running in a standalone program and not
inside the Gremlin Console. Our attempts to save our data have to catch any exceptions that may
occur. This code is also included as part of the CreateGraph.java sample program.
 
 

 

 

 
posted on 2022-05-01 12:25  bokeyuannicheng0000  阅读(41)  评论(0)    收藏  举报