Joan
Joan

Reputation: 4300

Gremlin groupBy in Java

Let's say I have this graph:

     +--own--> [made=Renault] --------------is-------------+
[name=Joan]                                                v
     +--own--> [made=Citroen] --is--> [type=Gas]      [type=Diesel]
     +--own--> [made=Peugeot] --is--> [type=Gas]           ^
[name=Jacky] --own--> [made=Opel] ------------is-----------+

Now I want to generate a list of the Type(s) of fuel that each Person own.

| Person |  Type  |
+--------+--------+  
| Joan   | Gas    |
| Joan   | Diesel |
| Jacky  | Diesel |

In SQL I would probably use ORDER BY on both the Type and the Name, but I don't understand in Gremlin how can I group them?

Upvotes: 1

Views: 338

Answers (1)

Paul Jackson
Paul Jackson

Reputation: 2147

The table pipe does this. I have your model loaded with an indexed property called "_stp_type" that contains the value "Person" for Joan and Jacky. I also have a "Name" property for them.

g.idx('entities')[['_stp_type':'Person']].Name.as('Owner').back(1).out('Owns').Type.as('Type').table().cap().next().unique()

That returns:

[
    [
        Jacky,
         Diesel
    ],
     [
        Joan,
         Diesel
    ],
     [
        Joan,
         Gas
    ]
]

The unique method (on the table class) is needed to prevent Joan from having a separate entry for each gas-powered vehicle.

Upvotes: 1

Related Questions