小精灵服务器返回的JSON取决于使用的查询,更具体来说,就是查询返回什么。在结果的数据部分返回的一切,多数情况下是一个数组。在数组内可以是一个简单的数字,或者是字符串。它也可以是字符串列表,或者包在表map中的其它的对象。如果您计划写一些通用的代码来处理不同的类型。就要知道它们看起来是什么样的。在下面这个例子中,笔者尝试展示一些您可能会遇到的结果。笔者主要关注的是JSON的一部分,这包含了数据的键。每个例子都假定:查询是使用HTTP协议发到到小精灵服务器的。通常,如果佻不确定一个特定的查询会生成什么JSON,您应可做一些实验,把它弄明白。
The JSON returned by the Gremlin Server depends on query that is used and more specifically,
what that query returns. Everything that is returned in the data part of the result will, at the
outermost level be an array. What is inside that array could be a simple number or a string. It could
also be a list of strings or other objects including maps. If you plan to write some general purpose
code that can handle the different possible formats it is important to know what they look like. In
the examples that follow I have attempted to show several of the possible response formats that
you may encounter. I am mainly going to focus of the parts of the JSON that follow the data key.
Each example assumes that the query shown was sent to a Gremlin Server using the HTTP protocol.
As always, if you are unsure what JSON a particular query may generate, you should always run
some experiments to find out.
请注意,下面的一些查询可能不是达到目标的最佳的方案。笔者故意地挑选了一些可以说晨小精灵不同步骤的查询,这样可以让您体会每种生成的JSON结果的类型。
Please note that some of the queries that follow may not represent the best way to achieve the
specific result. I have deliberately picked queries that show different Gremlin steps to give you a
feel for the type of JSON result each generates.
7.8.1. 没有结果No result
下面的查询没有返回任何结果。这个JSON说明了在这种情况下返回的数据会是一个空的列表[]
The following query does not return any results. The JSON reflects this in the form of the data
returned being an empty list "[]".
7.8.2. 整数结果Integer result
一个简单的查询,只返回一个整数结果,它会生成的JOSN如下所示。这个强求果的JSON部坠毁将包含一个有单精度整数值的数据部分并做为列表的一个成员。
A simple query that just returns a single integer result will generate JSON as shown below. The
result section of the JSON will contain a data section with the single integer value encoded as a list
with one member.
7.8.3.字符串结果 String result
和整数结果一样,只返回一个字符串结果的查询将会生成的JSON如下所示。结果的JSON部分将包含一个数据段它有一单个的字符串值并做为列表中的一个成员。
As with integer results, a query that just returns a single string result will generate JSON as shown
below. The result section of the JSON will contain a data section with the single string value
encoded as a list with one member.
7.8.4. 字符串列表 List of strings
下面的查询生成了一个数据数组,包含了代表机场代码的字符串列表。
The query below generates a data array containing a list of strings representing airport codes.
7.8.5. 整数列表 List of integers
下面的查询生成的数据数据包含了一个代表跑道数量的整数列表。注意实事上,您不需为此使用sack,一个简单的values步骤就可以生成相同的结果,笔者想举一个使用sack步骤的例子。
The query below generates a data array containing a list of integers representing runway counts.
Note that in reality you would not use a sack for this, a simple values step will generate the same
results, but I wanted to show an example that uses a sack step.
7.8.6. 混合类型的列表 List of mixed types
一个查询的结果包含多种不同的数据类型这是很常见的。下面的查询生成了一个列表,它包含字符串,整数和双精度数。注意,和我们前边见到的一样。TinkerPop 不会确保返回结果的顺序,所以,对此,别创建任何依赖。
It is common for a query result to contain a variety of different data types. The example below
generates a list containing a string, and integer and a double. Note, as we have seen before,
TinkerPop does not guarantee the order in which results are returned so do not create any
dependencies on that.
7.8.7. 值表 Value map
如您所料,在使用valueMap从一个查询生成结果时,JSON也可以包含一个表map.注意在列表哪每个属性值是如何编码的,即使是列表中只有一个值。
As you might expect, when a valueMap is used to generate the result from a query, the JSON
generated also contains a map. Note how each property value is encoded in a list even if there is
only one value.
7.8.8. 单个顶点Single vertex
当您的查询返回的是顶点,与在小精灵控制台不同,您会得到类似v[51]这样的返回;在与小精灵服务器会话时,您得到的是一个JSON对象,它代表了一切关于顶点的内容,包括它的ID,标签,属性和每个属性的ID。如果您不需要整个顶点返回,它可能需要您写一个查询,它只返回您所感兴趣的属性。如果佻的查询会呆能在结果中返回大量的顶点,这是很中肯的建议。
When your query returns a vertex, unlike in the Gremlin Console where you would get back
something like "v[51]" when talking to the Gremlin Server what you get back is a JSON object
representing everything that is known about the vertex including its ID , label, properties and the
ID of each property. If you do not need the entire vertex returned it might be worth writing your
query in a way such that you only get back the properties that you are interested in. This is
especially pertinent if your query could potentially return a lot of vertices in the result.
7.8.9. 选择顶点的信息Selected vertex information
削减我们得到的JSON的量的方法之一如下所示。让我们假设选择机场顶点,我们只对ID,机场代码和城市名感兴趣。我们可以构建一个查询,如下所示,它只返回每个顶点的这些值。
One way to limit the amount of JSON we get back is shown below. Let’s assume for a selection of
airport vertices, all we are interested in is the ID, airport code and city name. We can construct a
query, as shown below, that will return just those values for each vertex.
7.8.10. 单条边 Single edge
和我们查询一个顶点一样,当我们查询一条边时,我们会得到包括标签,ID和连接到边的顶点的信息等大量的信息。
Just as when we queried a single vertex, when we query a single edge, we get back a lot of
information including its label and ID and information about the vertices the edge is connected to.
7.8.11. 新的顶点 New vertex
当增加一个新顶点和一些属性时,返回的JSON会包含顶点的全部信息,包括ID,标签,类型和它的属性。
When a new vertex and some properties are added the returned JSON will contain all of the
information about the vertex including its ID, label and type as well as its properties.
7.8.12. 只返回新顶点ID New vertex only returning the ID
增加新顶点时,如果您对于返回整个新顶点的和属性不感兴趣,您可以写一下查询只返回新顶点的ID,如下所示。
When adding a new vertex, if you are not really interested in getting back the entire new vertex
and its properties, you can write the query to only return the ID of the new vertex as shown below.
7.8.13. 字符串列表值的路径 Path by value (list of strings)
下面的查询返回了机场代码的列表形式的两个机场之间的路径。注意当返回的JSON代表一条路径时,新对象的键。
The query below returns a path between two airports as a list of airport codes. Note the new objects
key that is used when the returned JSON represents a path.
7.8.14. 字符串和整数列表值的路径 Path by values (list of strings and integers)
和前边一个类似,这次路径包含了机场之间的距离。
Similar to the previous query but this time the path also includes the distance between the airports.
7.8.15. 两个顶点的路径 Two vertex path
下面的查询返回了路径,但是没有包含by调节器,这样返回的是两个顶点和它们的ID,标签和属性。
The query below returns a path but does not include a by modulator so what is returned is the two
vertices along with their IDs, labels and properties.
7.8.16. 有两个顶点和一条边的路径 Path with two vertices and an edge
下面的查询和前边的类似,但是包含了一条边。您可能希望在这看到,JSON会快速地变大,如查我们没有在查询中说明我们真正需要的返回结果。注意,因为是一个路径结果,如何把大部分数据包含在一个object键中。
The following query is similar to the previous one but also includes an edge. You can hopefully see
here how the JSON can rapidly get large if we are not more specific in our queries about what
results we really need back. Notice how, because this is a path result, most of the data is contained
inside an objects key.
7.8.17. 选定的表 Selection map
如果一个查询是以select步骤结属,它引用了前边定义的标签。返回的会是一个表map,标签是它的键,值是在查询中标签所附带的内容。
If a query ends with a select step that references labels defined earlier in the query, what is
returned is a map where the labels are the keys and the values are the things that the labels were
attached to in the query.
7.8.18. 映射表 Projected map
project步骤会生成一个表,和前边的select步骤一样。
The project step also generates a map just as the select step did in the previous example.
7.8.19. 字符串和一个表 Strings and a map
下面的路径查询返回了一个列表,包含了两个字符串:代表机场代码的和代表一条边的完整的JSON对象。
The following path query returns a list containing two strings representing airport codes and the
full JSON object representing an edge.
7.8.20. 嵌套列表 Nested lists
在使用控制台或者通过TinkerPop API 从一个应用程序执行小精灵命令时 一个查询以一个fold步骤结尾是一个很好的方式,可以把所有的结果都放到一个列表中。在小精灵服务器中工作时,以一个fold步骤结束一个查询在很多情况下会是多余的,因为结果总会放在JSON的一个列表里。在下面的例子中,在JSON装配时,fold步骤会使得外部的列表被嵌入到生成的另一个列表中。
When using the console or issuing Gremlin commands via the TinkerPop API from an application
program, ending a query with a fold step can be a nice way to put all the results into a list. When
working with a Gremlin Server, ending a query with a fold step in many cases is redundant as the
results will be placed in a list inside the JSON anyway. In the example below, the fold step simply
caused an extra list to be nested inside the one that was generated while the JSON was being
assembled.
浙公网安备 33010602011771号