如何使用Gremlin收集沿路径的所有顶点和边属性 [英] How to collect all vertex and edge properties along path, with Gremlin
问题描述
这是一个非常简单的查询:
Here's a really simple query:
g.V('customerId').out().path()
此的JSON输出是
{
"requestId":"96b26c1d-d032-2004-d36e-c700bd6db2a2",
"status":{
"message":"",
"code":200,
"attributes":{
"@type":"g:Map",
"@value":[
]
}
},
"result":{
"data":{
"@type":"g:List",
"@value":[
{
"@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":"customerId",
"label":"customer"
}
},
{
"@type":"g:Vertex",
"@value":{
"id":"e:customerIdemail@email.com",
"label":"email"
}
}
]
}
}
}
]
},
"meta":{
"@type":"g:Map",
"@value":[
]
}
}
}
现在,客户顶点还包含属性名称和年龄。我想了解的是,如何(如果可能的话)形成我的gremlin查询,以使其嵌套图内的顶点属性。请注意,当我只运行gV( customerId)时,响应确实包含这些属性。
Now, a customer vertex also contains the property name and age. What I would like to understand, is how to (simply, if possible) form my gremlin query such that it nests of the vertex properties within the graph. Note that when I just run g.V("customerId"), the response does contain these properties.
推荐答案
您应始终始终准确指定您想要遍历返回的数据。即使是简单的事情,也是如此:
You should always specific exactly the data that you want returned in a traversal. Even for something as simple as:
g.V('customerId')
您应该更喜欢:
g.V('customerId').valueMap('name','age')
在SQL中,实际上可能没有什么不同不会这样做
It's really no different in SQL where you likely wouldn't do
SELECT * FROM customer
,而是
SELECT name, age FROM customer
对于您的问题,您只需要指定要返回的数据,因此使用 by()
调制器,用于 path()
:
As for your question, you just need to specify the data you want back, so use the by()
modulator for the path()
:
g.V('customerId').
out().
path().
by(valueMap('name','age'))
当然您的 out()
也是客户,如果没有,只需添加第二个 by()
为此所需的字段。 by()
调制器以循环方式应用。如果您想使用稍微干净一点的JSON,可以改用 project()
,例如:
That of course assumes your out()
is also a "customer", if not, just add a second by()
with the specific fields required for that. The by()
modulators are applied in round-robin fashion. If you'd like a slightly cleaner bit of JSON to deal with you might instead use project()
like:
g.V('customerId').
out().
path().
by(project('name','age').
by('name').
by('age'))
那样会杀死 valueMap()
添加的嵌入列表,以便正确地考虑多个-properties。
as that will kill out the embedded lists that valueMap()
adds in to properly account for multi-properties.
从TinkerPop 3.4.4开始,您还可以考虑 elementMap()
步骤,其中包括更多
As of TinkerPop 3.4.4, you would also consider elementMap()
-step which includes more of the structure of the graph element.
gremlin> g.V().has('person','name','marko').elementMap()
==>[id:1,label:person,name:marko,age:29]
gremlin> g.V().has('person','name','marko').elementMap('name')
==>[id:1,label:person,name:marko]
gremlin> g.V().has('person','name','marko').properties('name').elementMap()
==>[id:0,key:name,value:marko]
gremlin> g.E(11).elementMap()
==>[id:11,label:created,IN:[id:3,label:software],OUT:[id:4,label:person],weight:0.4]
这篇关于如何使用Gremlin收集沿路径的所有顶点和边属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!