为什么 Gremlin 函数 fold() 会影响 JSON 响应中的结果?
Why does Gremlin function fold() affect the result in JSON response?
折叠如何影响 gremlin 服务器 JSON 的输出?当我展开和折叠路径内容时,我得到不同的数据结构,它添加了边和顶点属性。虽然我的目标也是获取路径中的属性,但这似乎很奇怪,而且我在文档中找不到有关此功能的信息。
那么为什么会这样呢?
g.V('1').out().path()
g.V('1').out().path().by(unfold().fold())
当我运行以下查询时:g.V('1').out().path()
{
...
{
"@type": "g:Path",
"@value": {
"labels": {
"@type": "g:List",
"@value": [
{
"@type": "g:Set",
"@value": []
},
{
"@type": "g:Set",
"@value": []
}
]
},
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER"
}
},
{
"@type": "g:Vertex",
"@value": {
"id": "2",
"label": "USER"
}
}
]
}
}
}
...
}
但是当我g.V('1').out().path().by(unfold().fold())
{
...
{
"@type": "g:Path",
"@value": {
"labels": {
"@type": "g:List",
"@value": [
{
"@type": "g:Set",
"@value": []
},
{
"@type": "g:Set",
"@value": []
}
]
},
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER",
"properties": {
"prop1": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 101839172
},
"value": {
"@type": "g:Int32",
"@value": 1
},
"label": "prop1"
}
}
],
"created_at": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 589742877
},
"value": {
"@type": "g:Date",
"@value": 1557226436119
},
"label": "created_at"
}
}
]
}
}
}
]
},
{
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "2",
"label": "USER",
"properties": {
"prop1": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": -1354828672
},
"value": {
"@type": "g:Date",
"@value": 1557225020168
},
"label": "prop1"
}
}
],
"created_at": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 589742878
},
"value": {
"@type": "g:Date",
"@value": 1557226436119
},
"label": "created_at"
}
}
]
}
}
}
]
}
]
}
}
}
...
}
编辑:附加信息,我发现除了 fold()
之外,我还可以使用 project()
和 identity()
获得具有属性的整个实体。
所以当我 运行 g.V('1').out().path().by(identity())
我得到路径的以下内容,与第一个查询相同。
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER"
}
},
{
"@type": "g:Vertex",
"@value": {
"id": "2",
"label": "USER"
}
}
]
}
但是当我 运行 g.V('1').out().path().by(project('identity').by(identity()))
时,这是我在路径中得到的(注意属性对象):
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:Map",
"@value": [
"identity",
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER",
"properties": {
"prop1": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 101839172
},
"value": {
"@type": "g:Int32",
"@value": 1
},
"label": "prop1"
}
}
],
"created_at": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 589742877
},
"value": {
"@type": "g:Date",
"@value": 1557226436119
},
"label": "created_at"
}
}
],
}
}
}
]
}
您永远不应该获取从服务器返回的任何图形元素(即 Vertex
、Edge
或 VertexProperty
)的属性 - 只有一个 "reference" 由id
和 label
。因此,您在第一次遍历中看到的是正确的,而在使用 by(unfold().fold())
的第二次遍历中看到的是错误的。
这实际上是我创建的 TinkerPop 中的一个错误 TINKERPOP-2212。
获得你想要的东西的正确方法是按照以下方式做一些事情:
gremlin> g.V(1).out().path().by(valueMap())
==>[[name:[marko],age:[29]],[name:[lop],lang:[java]]]
==>[[name:[marko],age:[29]],[name:[vadas],age:[27]]]
==>[[name:[marko],age:[29]],[name:[josh],age:[32]]]
gremlin> g.V(1).out().path().by(valueMap(true).by(unfold()))
==>[[id:1,label:person,name:marko,age:29],[id:3,label:software,name:lop,lang:java]]
==>[[id:1,label:person,name:marko,age:29],[id:2,label:person,name:vadas,age:27]]
==>[[id:1,label:person,name:marko,age:29],[id:4,label:person,name:josh,age:32]]
或者在最新版本的 TinkerPop 中,将 valueMap(true)
替换为:
gremlin> g.V(1).out().path().by(valueMap().by(unfold()).with(WithOptions.tokens))
==>[[id:1,label:person,name:marko,age:29],[id:3,label:software,name:lop,lang:java]]
==>[[id:1,label:person,name:marko,age:29],[id:2,label:person,name:vadas,age:27]]
==>[[id:1,label:person,name:marko,age:29],[id:4,label:person,name:josh,age:32]]
折叠如何影响 gremlin 服务器 JSON 的输出?当我展开和折叠路径内容时,我得到不同的数据结构,它添加了边和顶点属性。虽然我的目标也是获取路径中的属性,但这似乎很奇怪,而且我在文档中找不到有关此功能的信息。
那么为什么会这样呢?
g.V('1').out().path()
g.V('1').out().path().by(unfold().fold())
当我运行以下查询时:g.V('1').out().path()
{
...
{
"@type": "g:Path",
"@value": {
"labels": {
"@type": "g:List",
"@value": [
{
"@type": "g:Set",
"@value": []
},
{
"@type": "g:Set",
"@value": []
}
]
},
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER"
}
},
{
"@type": "g:Vertex",
"@value": {
"id": "2",
"label": "USER"
}
}
]
}
}
}
...
}
但是当我g.V('1').out().path().by(unfold().fold())
{
...
{
"@type": "g:Path",
"@value": {
"labels": {
"@type": "g:List",
"@value": [
{
"@type": "g:Set",
"@value": []
},
{
"@type": "g:Set",
"@value": []
}
]
},
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER",
"properties": {
"prop1": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 101839172
},
"value": {
"@type": "g:Int32",
"@value": 1
},
"label": "prop1"
}
}
],
"created_at": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 589742877
},
"value": {
"@type": "g:Date",
"@value": 1557226436119
},
"label": "created_at"
}
}
]
}
}
}
]
},
{
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "2",
"label": "USER",
"properties": {
"prop1": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": -1354828672
},
"value": {
"@type": "g:Date",
"@value": 1557225020168
},
"label": "prop1"
}
}
],
"created_at": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 589742878
},
"value": {
"@type": "g:Date",
"@value": 1557226436119
},
"label": "created_at"
}
}
]
}
}
}
]
}
]
}
}
}
...
}
编辑:附加信息,我发现除了 fold()
之外,我还可以使用 project()
和 identity()
获得具有属性的整个实体。
所以当我 运行 g.V('1').out().path().by(identity())
我得到路径的以下内容,与第一个查询相同。
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER"
}
},
{
"@type": "g:Vertex",
"@value": {
"id": "2",
"label": "USER"
}
}
]
}
但是当我 运行 g.V('1').out().path().by(project('identity').by(identity()))
时,这是我在路径中得到的(注意属性对象):
"objects": {
"@type": "g:List",
"@value": [
{
"@type": "g:Map",
"@value": [
"identity",
{
"@type": "g:Vertex",
"@value": {
"id": "1",
"label": "USER",
"properties": {
"prop1": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 101839172
},
"value": {
"@type": "g:Int32",
"@value": 1
},
"label": "prop1"
}
}
],
"created_at": [
{
"@type": "g:VertexProperty",
"@value": {
"id": {
"@type": "g:Int32",
"@value": 589742877
},
"value": {
"@type": "g:Date",
"@value": 1557226436119
},
"label": "created_at"
}
}
],
}
}
}
]
}
您永远不应该获取从服务器返回的任何图形元素(即 Vertex
、Edge
或 VertexProperty
)的属性 - 只有一个 "reference" 由id
和 label
。因此,您在第一次遍历中看到的是正确的,而在使用 by(unfold().fold())
的第二次遍历中看到的是错误的。
这实际上是我创建的 TinkerPop 中的一个错误 TINKERPOP-2212。
获得你想要的东西的正确方法是按照以下方式做一些事情:
gremlin> g.V(1).out().path().by(valueMap())
==>[[name:[marko],age:[29]],[name:[lop],lang:[java]]]
==>[[name:[marko],age:[29]],[name:[vadas],age:[27]]]
==>[[name:[marko],age:[29]],[name:[josh],age:[32]]]
gremlin> g.V(1).out().path().by(valueMap(true).by(unfold()))
==>[[id:1,label:person,name:marko,age:29],[id:3,label:software,name:lop,lang:java]]
==>[[id:1,label:person,name:marko,age:29],[id:2,label:person,name:vadas,age:27]]
==>[[id:1,label:person,name:marko,age:29],[id:4,label:person,name:josh,age:32]]
或者在最新版本的 TinkerPop 中,将 valueMap(true)
替换为:
gremlin> g.V(1).out().path().by(valueMap().by(unfold()).with(WithOptions.tokens))
==>[[id:1,label:person,name:marko,age:29],[id:3,label:software,name:lop,lang:java]]
==>[[id:1,label:person,name:marko,age:29],[id:2,label:person,name:vadas,age:27]]
==>[[id:1,label:person,name:marko,age:29],[id:4,label:person,name:josh,age:32]]