简体   繁体   English

Gremlin查询,返回键/值列表,其中key是顶点ID,而value是特定属性的值

[英]Gremlin query that returns key/value list where key is the vertex ID and value is value of a specific property

I am starting to learn graph databases and have hit a small snag with a query in the Gremlin query language for a proof of concept. 我开始学习图形数据库,并用Gremlin查询语言进行查询以寻求概念证明。

Say I have a vertex that represents a specific type of bolt and each of the properties represent a material and cost that the bolt is available in. 假设我有一个表示特定类型螺栓的顶点,并且每个属性都表示该螺栓可用的材料和成本。

id: bolt-123,
label: part,
properties: [
    { steel : 0.05 },
    { aluminum : 0.02 },
    { iron : 0.03 },
    { plastic : 0.01 }
]

I would like to be able to get a list of all bolts and their cost that are made out of plastic. 我希望能够获得所有由塑料制成的螺栓及其成本的清单。 With my currently limited knowledge of Gremlin, I have been able to come up with the following query: 以我目前对Gremlin的有限了解,我已经能够提出以下查询:

g.V().hasLabel('part').has('plastic').project('key', 'value').by('id').by('plastic')

which results in 导致

[ { "key": "bolt-123", "value": 0.01 },
  { "key": "bolt-456", "value": 0.02 } ]

While this query makes perfect sense to me, I am hoping to flatten this out a little bit more so that I could have: 尽管此查询对我来说很有意义,但我希望将其简化一些,以便可以:

[ { "bolt-123", 0.01 },
  { "bolt-456", 0.02 } ]

Thanks for helping a Gremlin newbie out. 感谢您帮助Gremlin新手。

If you just want the values of the Map you can append select(values) to the end of that traversal and it will drop the keys. 如果只需要Map的值,则可以将select(values)追加到遍历的末尾,它将删除键。 I'll use the "modern" graph as an example: 我将以“现代”图为例:

gremlin> g.V().project('k','v').by(id).by('name').select(values)
==>[1,marko]
==>[2,vadas]
==>[3,lop]
==>[4,josh]
==>[5,ripple]
==>[6,peter]

Of course, if you don't need the Map then I wouldn't bother to project() it in the first place, just create the List of values instead: 当然,如果您不需要Map那么我一开始就不必费心project() ,只需创建值List

gremlin> g.V().map(union(id(),values('name')).fold())
==>[1,marko]
==>[2,vadas]
==>[3,lop]
==>[4,josh]
==>[5,ripple]
==>[6,peter]

If you want the exact output you presented you could coerce the List to a Map with group() : 如果您想要显示的确切输出,则可以使用group()List强制转换为Map

gremlin> g.V().
......1>   map(union(id(),values('name')).
......2>       fold().
......3>       group().
......4>         by(limit(local,1)).
......5>         by(tail(local)))
==>[1:marko]
==>[2:vadas]
==>[3:lop]
==>[4:josh]
==>[5:ripple]
==>[6:peter]

it's basically taking each List and taking the key for the Map from the first item in the list with limit(local,1) and then taking the value from the second item in the list with tail(local) . 它基本上是获取每个List并从列表中的第一项以limit(local,1)获取Map的键,然后从列表中的第二项以tail(local)

Of course, it occurs to me now that if your "id" is truly unique then you could omit the union() and just do group() : 当然,现在我想到的是,如果您的“ id”确实是唯一的,那么您可以省略union()而只需执行group()

gremlin> g.V().group().by(id()).by(values('name').unfold())
==>[1:marko,2:vadas,3:lop,4:josh,5:ripple,6:peter]

Note that I purposefully did by(values('name').unfold()) instead of by('name') because the latter would wrap the result in a List . 请注意,我故意by(values('name').unfold())代替了by('name')因为后者会将结果包装在List

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM