How to get id and all properties from a vertex in gremlin?
Asked Answered
D

2

9

I am using AWS Neptune with gremlin and I want to get the id of a vertex with all properties of the vertex in one query. How can I do this?

I have tried

g.V().hasLabel('file').valueMap(true) 

but the output is:

{ "fileSize": [ "9170" ], "Gremlin.Net.Process.Traversal.T": "f1fce58306f85ca7050503160640d735c9919c8fc85881d65de80bfe31b5ca24", "mimeType": [ "text/html" ] }

No label and no id is there. The problem with

project('id','label',' fileSize', 'mimeType', 'malwareSource').
    by(id).
    by(label).
    by('fileSize').
    by('mimeType').
    by('malwareSource') 

is that the propertie malwareSource is sometimes part of a file vertex an sometimes not. So if there is no malwareSource property an exception is thrown.

Debag answered 3/4, 2019 at 12:20 Comment(0)
E
15

There are lots of ways, but generally use valueMap():

g.V(1).valueMap(true)

In TinkerPop 3.4.0+, the output is a bit better controlled with the addition of by():

g.V(1).hasLabel("person").valueMap().by(unfold()).with(WithOptions.ids)

You could also use project() in various ways, but that requires you to know all the keys you wish to grab. Typically, you should know your keys anyway.

g.V(1).
  project('id','label',' fileSize', 'mimeType', 'malwareSource').
    by(id).
    by(label).
    by('fileSize').
    by('mimeType').
    by('malwareSource')

If a property value is optional to a vertex then just create an if-then condition in the by() modulator:

g.V(1).
  project('id','label',' fileSize', 'mimeType', 'malwareSource').
    by(id).
    by(label).
    by('fileSize').
    by('mimeType').
    by(coalesce(values('malwareSource'),constant('N/A'))
Electrode answered 3/4, 2019 at 13:9 Comment(9)
But I do not want to delete or remove the properties. I want to get them in the result of the query together with the IDDebag
whoa - i really misread your question. sorry - updated my answer.Electrode
g.V().hasLabel('file').valueMap(true) returns: { "fileSize": [ "9170" ], "Gremlin.Net.Process.Traversal.T": "f1fce58306f85ca7050503160640d735c9919c8fc85881d65de80bfe31b5ca24", "mimeType": [ "text/html" ] } there is no id and no labelDebag
g.V(1).hasLabel('file').valueMap().by(unfold()).with(WithOptions.ids) throws an syntax error exception in azure and in neptuneDebag
how can I use project if I neet the id, the label and following properties: fileSize, mimeType, malwareSource?Debag
added an example, but valueMap(true) should work fine for Neptune. i will point someone with more neptune knowledge than me at this question in case there is some sort of bugElectrode
I have edit my post and explained why the answers do not fix the problem.Debag
updated my answer again - basically, by() can take an anonymous Traversal as an argument so you can add more Gremlin there to account for the optional property key. In this case I used coalesce() but you could use optional(), choose(), etc. as well depending on your needs. i'm really curious about valueMap() on Neptune though - interesting that it holds different behavior there. i wasn't aware of that.Electrode
Using valueMap(true) should work fine with Neptune. Here is an example from one of my graphs from the Gremlin Console. gremlin> g.V('3').valueMap(true) ==>{curDelay=[0], country=[US], code=[AUS], longest=[12250], city=[Austin], lon=[-97.6698989868164], type=[airport], label=airport, elev=[542], icao=[KAUS], id=3, runways=[2], region=[US-TX], lat=[30.1944999694824], desc=[Austin Bergstrom International Airport]}. Does it work for you from the Console? Also has the latest maintenance been applied to your Neptune cluster? Just want to verify you and I are using the same level.Curlew
I
5

Now, you can use elementMap()

g.V(1).elementMap()
Instrumentalist answered 19/9, 2022 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.