gremlin - 为什么 Gremlin 函数 fold() 会影响 JSON 响应中的结果?
问题描述
折叠如何影响 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"
}
}
]
}
}
}
]
}
]
}
}
}
...
}
编辑:附加信息,我发现除了 之外,我可以使用andfold()
获取具有属性的整个实体。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
组成的“引用” 。因此,您在第一次遍历中看到的内容是正确的,而您在第二次使用中看到的内容是错误的。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]]
推荐阅读
- c++ - 为什么我的 C++ 程序的汇编输出充满了没有汇编代码的 .ascii?
- python - 根据实例变量对对象数组进行排序
- azure-media-services - Azure 媒体服务:媒体/视频无法在 Safari/Edge 浏览器上解密,但在 Chrome 中运行良好
- azure-active-directory - 需要用户登录才能使用 Bot Framework 和 AAD OAuth2
- mongodb - 如何不匹配 MongoDB 内元素子数组中的文档
- r - 尝试合并两个不同长度的数据集(在 R 中使用 Combine()
- typescript - TypeScript 对象属性需要重复定义
- scala - 将包含字符串的列转换为包含 Apache Spark UDF 中的对象列表的列
- javascript - 如何对二维数组进行分组并将其转换为具有键名的对象
- json - 如何在android studio中解析多个json数组