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.

Christopher
  • 91
  • 1
  • 1
  • 3

2 Answers2

14

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'))
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • 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 ID – Christopher Apr 03 '19 at 13:11
  • whoa - i really misread your question. sorry - updated my answer. – stephen mallette Apr 03 '19 at 14:27
  • g.V().hasLabel('file').valueMap(true) returns: { "fileSize": [ "9170" ], "Gremlin.Net.Process.Traversal.T": "f1fce58306f85ca7050503160640d735c9919c8fc85881d65de80bfe31b5ca24", "mimeType": [ "text/html" ] } there is no id and no label – Christopher Apr 03 '19 at 14:56
  • 2
    g.V(1).hasLabel('file').valueMap().by(unfold()).with(WithOptions.ids) throws an syntax error exception in azure and in neptune – Christopher Apr 03 '19 at 14:57
  • how can I use project if I neet the id, the label and following properties: fileSize, mimeType, malwareSource? – Christopher Apr 03 '19 at 15:00
  • 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 bug – stephen mallette Apr 03 '19 at 16:20
  • I have edit my post and explained why the answers do not fix the problem. – Christopher Apr 04 '19 at 08:05
  • 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. – stephen mallette Apr 04 '19 at 10:49
  • 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. – Kelvin Lawrence Apr 04 '19 at 21:04
3

Now, you can use elementMap()

g.V(1).elementMap()
Thirumal
  • 8,280
  • 11
  • 53
  • 103