下面是本书前边已经用过的一个查询,它找到了所有超过8000英里长的路径并返回了机场对和它们之间的距离。
Below is a query that we have seen used earlier in this book. It finds all routes longer than 8,000
miles and returns the airport pairs and the distance between them.

 使用附带廷克图的小精灵控制台我们运行这个查询,我们得到的返回结果已经在一定程度上由控制台进行了优化打印,如下所示。

When we run this query using the Gremlin console with TinkerGraph we get back results that have
been to a degree pretty printed by the Console as shown below.

 然而,如果您要使用返回的JSON应答,例如当使用一个小精灵服务器通过HTTP连接,您可能没有得到小精灵控制为您做的优化打印。相反,您可能得到的回来看起来更类似这样:来自相同的和我们上面用到的一样的查询。

However, if you were to use a system that returns the full JSON response, as is the case when using
a Gremlin Server over an HTTP connection, you will not get the benefit "pretty printing" that the
Gremlin Console does for you. Instead, you will get back something that looks a lot like this from
the exact same query as the one we used above.
 

 一些情况上,返回的东西,例如, 我们看到我们在查询中用到的标签a和b, 在这个例子中,我们真正想要的最最后的部分,机场代码和距离。我们可以决定写代码来处理这样的JSON(可能会用到一个JSON帮助类),这是一个您可以做的好的决定。然而,通过微调查询,我们也可以让小精灵为我们返回我们真正需要的。让我们开始看看如果我们增加.toList().toString() 到查询的结尾会发生什么。看下面修改后的查询。

What is being returned is useful in some cases, for example we can see the a and b labels that we
used in our query but in this case all we really wanted was the last part with the airport codes and
the distances. We could decide to write code to process this JSON as-is (probably using a JSON
helper class) and that is a valid choice you could make. However by tweaking the query slightly, we
can enable Gremlin to give us back what we really wanted. Let’s start by looking at what happens if
we add .toList().toString() to the end of the query. Take a look at the modified form of the query
below.

 如果我们把这个修改格式的查询发送到小精灵服务器, 我们应当得到一些更类似我们从小精灵控制台处理时返回的结果。如下所示,它的确更易于在您的应用程序中处理了。然而,这仍然不是一个理想的结果,我们现在有了一个列表,它包含了所有我们的路径的单一的字符串。

If we were to send this modified form of the query to our Gremlin Server, we should get back
something that looks a lot more like the result we got back when working with the Gremlin Console.
As shown below, it is certainly a bit easier to process in your application now. However, this is still
not an ideal result as what we now have is a list containing a single string with all of our routes in
it.

 我们可以再晕一步后处理,从而拉把字符串拆分到字符串列数组中,在那每个字符串都是一个[AKL,9025,DOH]这样形式的路径。方法之一是截取字符串最后不需要的字符,然后使用split分配它。在字符串中有大量的逗号,笔者不用只是做一个简单的split(",") ,因为这不会返回笔者需要的。为了y让split奏效,笔者用]x 代替每个出现的], 然后再用split("x").来拆分。这是修改后的查询。

We can add a little more post processing to split up our single string into an array of strings where
each string is a single route of the form [AKL,9025,DOH]. One way to do this is to trim off the
unwanted characters at each end of the string and then use split to divide it up. As there are a lot of
commas in the string I could not just do a simple split(",") as that would not have returned what I
wanted. To make the split work, I replaced every occurence of ], in the string with ]x and then did
the split using split("x"). Here is the modified query.

 这是我们现在从返回的JSON中得到的。每个路径现在就是一个在字符数组中的字符串。到此提取每条路径的机场各和距离就是一个简单的任务了。

Here is what we now get back in the returned JSON. Each route is now a string in an array of
strings. From here it is a simple task to extract the airport names and distances for each route.

 这完全是一个个人偏好,无论您决定查询返回更少的数据,还是从最初的查询中返回我们得到的全部数据集。 让查询限制返回更少的数据它的好处之一是,可能有大量更少的数据。需要返回到您的应用程序,存储在内存或磁盘。然而,最大多数编程语言有内置的支持,可以轻松反串型化JSON对象,可以让它为您做其余的处理。

It’s really a matter of personal preference whether you decide to have the query return less data or
just return the full set of data that we got back from the initial query. One advantage to having the
query limit what is returned is that less data, potentially a lot less data, will need to be sent back to
your application and stored in memory or on disk. However, as, most programming languages have
built in support that makes it easy de serialize JSON objects into native data structures such as
maps, you may prefer to just have all the JSON be returned and do the rest of the processing
yourself.
举个简单的例子,如果我们向我们前一节中创建的Ruby应用程序中增加下面的代码,使用我们增加后处理前的那个原始的查询,我们可以轻松得到我们感兴趣的那些部分JSON。
By way of a simple example, if we added the following lines to our Ruby application that we created
in the previous section, and used the original query from before we added any post processing, we
could easily get at the parts of the JSON that we are interested in.

 

代码使用了Ruby 的JSON类来把从小精灵服务器得到的JSON应答转化到表map数据结构中。我们可以通过JSON中的名字访问map表的每个部分。注意代码期望特定的一些关键字出现在JSON中。不是所有查询结果都包含了这些关键字。可以做更少的工作就把它转化为一个更通用的代码。它可以处理服务器返回给我们的任何可能的JSON格式。这是运行更新的Ruby代码得到的输出。注意我们现在有了一些优化的列表,每个列表都包含两个字符串和一个整数。数据现在的格式易于进一步地处理。

The code uses Ruby’s JSON class to convert the JSON response from the Gremlin Server into a map
data structure. We can then access each part of the map by the names contained in the JSON. Note
that the code as written expects a specific set of keywords to be present in the JSON. Not all query
results contain these keywords. Therefore, it would take a little more work to turn this into a more
general purpose piece of code that could handle any of the possible JSON return formats the server
could send to us. Here is the output from running the updated Ruby code. Notice that what we have
now is a nice collection of lists, each one containing two strings and an integer. The data is now in a
form that is really easy and convenient to process further.

 下一节您会找到更多由小精灵服务器返回的JSON的例子。也有一些例子说明了如何削减返回数据的量。

In the next section you will find more examples of the JSON that can be returned by Gremlin Server
and also some examples of how to reduce the amount of data that is returned.
posted on 2022-05-05 11:09  bokeyuannicheng0000  阅读(53)  评论(0)    收藏  举报