0

This question is related to this post that Kelvin Lawrence answered very helpfully, wanted to post it as a separate question bec the first question was answered well already.

From this query:

g.V('a2661f57-8aa7-4e5c-9c89-55cf9b7e4cf8').as('self')
.sideEffect(out('rated').store('movies')) 
.out('friended')
.group() 
.by(valueMap(true).by(unfold())) 
.by(out('rated').where(within('movies')).count()) 
.order(local) 
.by(values,desc) 
.unfold()
.select(keys)

i get this result:

1   {<T.id: 1>: 'fdc45bd3-be08-4716-b20f-b4f04987c5e0', <T.label: 4>: 'user', 'username': 'elin102dev', 'name': 'elin obrien', 'avatarUrl': 'public/avatars/fdc45bd3-be08-4716-b20f-b4f04987c5e0.jpg'}
2   {<T.id: 1>: 'bbf1b0db-68cc-41f1-8c7a-5fd77b698e39', <T.label: 4>: 'user', 'username': 'iris', 'name': 'Iris Ebert', 'avatarUrl': 'public/avatars/bbf1b0db-68cc-41f1-8c7a-5fd77b698e39.jpg'}
3   {<T.id: 1>: '34c2ea80-4f84-4652-a7c3-48ce43d9aea7', <T.label: 4>: 'user', 'username': 'iris103dev', 'name': 'iris obrien', 'avatarUrl': 'public/avatars/34c2ea80-4f84-4652-a7c3-48ce43d9aea7.jpg'}

I want to convert the T.id and T.label values in the response to simply "id" and "label". Kelvin, if you're reading this, i i tried appending the following to the query above but it returns 0 results:

.select('id', 'label', 'username', 'name', 'avatarUrl') 
    .by(T.id)
    .by(T.label)
    .by('username')
    .by('name')
    .by('avatarUrl')
    .toList()

I could use a little more help figuring this out, not having much success. Thanks in advance.

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
Programjon
  • 23
  • 6
  • Are you trying to do this from code, or from somewhere like the Neptune notebooks? From the output I suspect the notebooks. Selecting a `T.id` is straightforward in code but can be more tricky when sending scripts to a server. – Kelvin Lawrence Feb 10 '22 at 16:36
  • It might be easier to reformulate the query such that you `project` the `Id` value into your own map. – Kelvin Lawrence Feb 10 '22 at 16:42
  • @kelvin-lawrence - I am using a jupyter notebook in neptune, yes. i had the same notion about doing a project, but when i try to use a project on the current query like so (after the select(keys)) i can get this to work: – Programjon Feb 10 '22 at 17:12
  • .project('username', 'name', 'avatarUrl') .by('username') .by('name') .by('avatarUrl') – Programjon Feb 10 '22 at 17:12
  • but not this: .project('id', 'label', 'username', 'name', 'avatarUrl') .by(T.id) .by(T.label) .by('username') .by('name') .by('avatarUrl') – Programjon Feb 10 '22 at 17:12
  • the 2nd example gives me the error - Received error message '{'requestId': 'ab405439-fc57-4f71-b9c9-758fea9dc63d', 'status': {'message': '{"detailedMessage":"java.util.LinkedHashMap cannot be cast to org.apache.tinkerpop.gremlin.structure.Element","requestId":"ab405439-fc57-4f71-b9c9-758fea9dc63d","code":"UnsupportedOperationException"}', 'code': 498, 'attributes': {}}, 'result': {'data': None, 'meta': {}}}' – Programjon Feb 10 '22 at 17:13
  • Yes - I think you will need to project the ID into a map, not from it. `select(T.id)` does not work. So it would be something like `project('id').by(id)` while building up a map. – Kelvin Lawrence Feb 10 '22 at 17:36
  • so you mean projecting it on the valueMap line like this: .by(valueMap(true).project('id').by(id)? i tried that and got the same error as above :( thanks for your responses. – Programjon Feb 10 '22 at 17:46
  • No, not quite, I'm suggesting to just group initially by the vertex itself and then right at the end, when you need the properties, project them out. – Kelvin Lawrence Feb 10 '22 at 17:54
  • I'll add an answer below – Kelvin Lawrence Feb 10 '22 at 18:00

1 Answers1

3

It is not possible to do a select(T.id) inside a query. In code if you get a map back you can access the T.id field. For a case such as this, it is better to delay fetching the properties until you finally need them. You might try rewriting the query like this.

g.V('a2661f57-8aa7-4e5c-9c89-55cf9b7e4cf8').as('self').
  sideEffect(out('rated').store('movies')). 
  out('friended').
  group(). 
    by(). 
    by(out('rated').where(within('movies')).count()). 
  order(local). 
    by(values,desc). 
    unfold().
  select(keys).
  project('id','label','username').
    by(id).
    by(label).
    by('username')
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38