我们有了一个小精灵服务器启动并运行着,且支持HTTP和WebSockets连接,如果我们原意我们主可以与它通信,最多命最一个curl命令。下面的curl命令使用一个HTTP GET 发送一个请求到我们的小精灵服务器。
Now that we have a Gremlin Server up and running that supports both HTTP and Web Sockets
connections, we can, if we wish, communicate with it using nothing more than a curl command.
The curl command below uses an HTTP GET to send a query to our Gremlin Server.

 

做为对于HTTP GET请求的响应,服务器发送回来的结果打包成了下面这样的JSON。笔者忆将输出格式化,以方便阅读。笔者真正得到的返回没有一个换行分段,它相当的难以阅读。
In response to the HTTP GET request the server sends back the result packaged as JSON as follows. I
have formatted the output in a way that makes it easier to read. What was actually returned did not
have any line breaks in it at all and was quite hard to read..

 下面的例子说明了如何发送相同的查询,但是valueMap步骤被移除了,使用了HTTP POST。 阿帕奇TinkerPop文档中提到使用POST是通过HTTP向小精灵服务器发送请求的推荐的方法。注意在这个例子中我们是把请求打包成了JSON,这样就可以免去引用字符。

The following example shows how to send the same query, but with the valueMap step removed,
using an HTTP POST. The Apache TinkerPop documentation states that using POST is the
recommended way to send queries over HTTP to a Gremlin Server. Note how in this case we are
sending the query packaged as JSON and that we have to escape the quote characters.

和前一个查询一样,HTTP POST形式的查询也会把返回结果打包成JSON格式。然而在这个例子中,因为笔者去掉了value Map步骤,JSON包含了顶点和它的属性的额外的信息,这些信息的形式是:ID值,标签。这是因为这次结果代表了一个顶点而不是一个表map.为了易于阅读,笔者再次让输出格式化。

As with the prior query, the HTTP POST form of the query also returns the result packaged as JSON.

However, in this case, because I left off the valueMap step, the JSON includes additional information
in the form of the ID values and labels for the vertex and its properties. This is because the result
represents a vertex this time rather than a map. I have again formatted the output in a way that is
easier to read.
 

 笔者包含了不同种类的您需要处理的JSON结果,您可以在“从小精灵服务器返回的JSON的更多例子”这一节中看到。这些很快就出现啦。

I have included examples of the different types of JSON result that you are likely to have to process
in the "More examples of the JSON returned from a Gremlin Server" section that is coming up soon.
7.4. 使用withRemote从Java应用程序连接到小精灵服务器 Connecting to a Gremlin Server from Java using withRemote
直接处理小精灵服务器返回来的JSON结果这是非常有可能的。更理想的情况是把结果直接放进一个恰当类型的变量中。如果存在适合编程语言的小精灵驱动程序,那您就可以使用它,这是相当容易设置的。在本节我们将一些从Java应用程序连接到小精灵服务器,利用TinkerPop提供的withRemote的能力。
While it is perfectly possible to work directly with the JSON returned from a Gremlin Server it is
often more desirable to have the results placed directly into variables of the appropriate type. If the
appropriate Grelmin language driver exists for the programming language that you are using, this
is quite easy to setup. In this section we will look at connecting to a Gremlin Server from a Java
application and taking advantage of the withRemote capability that TinkerPop provides.
本节所示的例子的源代码可以来自这个位置at https://github.com/krlawrence/graph/tree/master/sample-code.的RemoteClient.java 类
The source code for the example shown in this section comes from the
RemoteClient.java sample located at https://github.com/krlawrence/graph/tree/
master/sample-code.
在下接下来的小节中我们将看到一些小的应用。首行需要导入所需的类,我们将需要连接服务器,检查查询结果。
Let’s look at the small application in sections. First of all it is necessary to import the required
classes that we will need to make the connection to the server and retrieve the query results.

 

我们现在可以定义一个小的Java类,我们称之为 RemoteClient ,设置到小精灵服务器的连接。这是通过创建一个Cluster.Builder 实例完成的。它将用于描述我们要连接的服务器和我们要使用的协议。重要的是:这些设置要与小精灵服务器使用的配置相匹配。对于这个简单的例子我们只使用localhost做为主机名,但是如果您要访问其它小精灵服务器,可以用服务器的名称来代替localhost。小精灵服务器默认的端是是8182,GyroMessageServializerV1d0串形化格式已选定。再次提示,这个要与协议和小精灵服力器支持的协议版本相匹配。
We can now define a small Java class that we will call RemoteClient and setup the connection to the
Gremlin Server. This is done by first of all creating a Cluster.Builder instance that will be used to
describe the server we are connecting to and the protocol we want to use. It is important that these
settings match what the Gremlin Server is configured to use. For this simple example we are just
using localhost as the host name but the name of any Gremlin Server that you have access to can be
used instead. The default Gremlin Server port of 8182 is specified and the
GyroMessageServializerV1d0 serialization format is selected. Again, this needs to match both the
protocol and the version of the protocol that your Gremlin Server is supporting.

 Cluster.Builder 实例启动了,我们就可以用它来创建我们的 Cluster 实例了。

Once the Cluster.Builder instance has been setup we can use it to create our Cluster instance.

 最后,我们需要为我们要处理的图所在的小精灵服务器设置GraphTraversalSource对象。TinkerPop文档推荐创建遍历时,使用EmptyGraph实例。此后,可以调用withRemote方法来建立远程连接。注意我们刚创建的cluster实例被做为一个参数传递。这看起来有点复杂,实际上它和我们使用控制台连接本地的图没太多不同。通过这种方式设置远程连接的唯一的不同是:当我们向图发起一个查询时,得到的返回不是JSON对象,结果会自动串形化到Java变量中。这使得我们的代码更易编写。从这点上说,这与我们直接连接处理本地的图,代码是相同的。

Lastly, we need to setup a GraphTraversalSource object for the Gremlin Server hosted graph that
we will be working with. The TinkerPop documentation recommends that an instance of an
EmptyGraph is used when creating the traversal. Having done that, the withRemote method can be
called to establish the remote connection. Note that the cluster instance that we just created is
passed in as a parameter. While this looks a little complicated it is really not a lot different than
when we connect to a local graph using the Gremlin Console. The only difference is that by setting
up the remote connection this way, when we start to issue queries against the graph, rather than
getting JSON objects back, the results will automatically be serialized into Java variables for us. This
makes our code a lot easier to write and essentially is the same code from this point onwards that
would also work with a local graph that we are directly connected to.

 

我们现要可以用新的图遍历源对象来发起一个小精灵查询。这个结果会直接进入名为vmaps的列表中。这个查询找到了地区代码是GB-ENG的前10个机场,GB-ENG 是英国的缩写。
We can now use our new graph traversal source object to issue a Gremlin query. The results will be
placed directly into the List called vmaps. The query finds the first 10 airports with a region code of
GB-ENG which is short for Great Britain - England.

 Java程序编译后,运行的输出看起来和上肌的相当相似。

When the Java application is compiled and run the output should look similar to that shown below.

 

 

 
posted on 2022-05-05 09:31  bokeyuannicheng0000  阅读(44)  评论(0)    收藏  举报