Search code examples
gremlintinkerpop3

Get map of vertex labels with all properties with Gremlin - Tinkerpop3


I am trying to return something like

{
  "label1" : ["prop1","prop2"],
  "label2" : ["prop3","prop4"],
  "label2" : ["prop1","prop3"]
}

etc where the labels[N] are vertex label values and the props array are the properties for these vertices.

I can get a list of labels and I can get a list of properties but I can't combine them in a single object. I could potentially do the two queries and combine the two arrays ultimately, but something like

g.V().valueMap().select(keys).dedup();

only gets properties where there are any, so if a vertex type doesn't have any properties the array returned by this is a different size than doing

g.V().label().dedup();

This is using gremlin syntax (TP3) Thanks


Solution

  • I'm assuming that you're trying to get sort of a schema definition. Note that this will be a fairly expensive traversal as you have to iterate all vertices to do this:

    gremlin> g.V().
    ......1>   group().
    ......2>     by(label).
    ......3>     by(properties().
    ......4>        label().
    ......5>        dedup().
    ......6>        fold())
    ==>[software:[name,lang],person:[name,age]]