4

In Tinkerpop3 valueMap is returning an array, how can I get a real key value pair (without Array)?

gremlin> Gremlin.version()
==>3.0.1-incubating

:> def trav = g.V().hasLabel('Group'); trav.valueMap()
==>{joinTime=[2016-12-05T22:27:01.088Z], groupId=[9de5-45cf-b40d-e357b40e87b1], mCanInvite=[true]}

:> def trav = g.V().hasLabel('Group'); trav.local(properties().group().by(key()).by(value()))
==>{joinTime={2016-12-05T22:27:01.088Z=1}, groupId={9de5-45cf-b40d-e357b40e87b1=1}, mCanInvite={true=1}
Vibgy
  • 577
  • 6
  • 15

4 Answers4

6

This is an expected behavior. See http://tinkerpop.apache.org/docs/current/reference/#valuemap-step

If you don't want the value to be wrapped in array, append .by(unfold()) to the valueMap()

In your case

def trav = g.V().hasLabel('Group');
trav.valueMap().by(unfold());
Vivek
  • 11,938
  • 19
  • 92
  • 127
  • 1
    In context of cosmos db germlin api grapgh database. This is not working and it has not supported by cosmos db "Error: Gremlin op does not support by(traversal)" – Gautam Sharma Mar 14 '22 at 08:54
1

You can now use .elementMap() and it does the same thing as .valueMap(true) except it doesn't return with arrays.

Spencer Sutton
  • 2,907
  • 2
  • 18
  • 19
  • 2
    With one caveat - if a property has multiple values (list or set cardinality) then `elementMap` will only return one of the entries. To get them all you will still need `valueMap` in that specific case. – Kelvin Lawrence Jan 24 '23 at 22:59
0

You can try to use select, it returns key-value pairs. For example:

g.V().hasLabel('Group').as('name','count')
    .select('name','count')
    .by('groupName')
    .by(outE('contains').count())

result will be something like:

[{'name':'group1', 'count':10},...]

name represents groupName property value, count - how many out edges has group.

user34813
  • 17
  • 1
  • 5
  • This is a reasonable fallback, but it does not work properly when some properties are optional. For example, if a particular vertex did not have a property called groupName by('groupName') will fail I think. – Vibgy Dec 06 '16 at 17:34
0

I faced the same issues, that i was not look., that i resolved to use the below queries.

g.V().hasLabel('user').project('id','firstName','age','userid',).by('id').by('firstName').by('age').by('userid')

Result :

[
  {
    "id": "thomas",
    "firstName": "Thomas",
    "age": 44,
    "userid": 1
  },
  {
    "id": "mary",
    "firstName": "Mary",
    "age": 39,
    "userid": 2
  }
]

Note - This will return error, if the vertex doesn't have the query's property. In my case of Azure Cosmos DB Gremlin API, all my documents as the properties.

when tried to query the cosmos db database, the .ValueMap properties works but it return the array

g.V().hasLabel('user').valueMap('firstName')

Result:

[
  {
    "id": [],
    "firstName": [
      "Thomas"
    ]
  },
  {
    "id": [],
    "firstName": [
      "Mary"
    ]
  }
]
Gautam Sharma
  • 851
  • 8
  • 13