create complex gremlin-java query

2019-06-04 07:30发布

问题:

I have model implemented in titan graph database with relations presented below:

[A] ---(e1)---> [B] <---(e2)--- [C] ---(e3)---> [D]
 |      |        |       |       |      |        |
prop:id |    prop:number |       | label:e3      |
        |                |    prop:id            |
   label:e1         label:e2                 prop:number
    prop:prop1

A and B are "main vertices" (for example users), vertices B and C are "less important vertices" describing some data connected with users.

The input for the query algorithm is property id of vertex A.

I want to find all such vertices D, that are connected with A in the manner shown above. What's more I want to remember the property prop1 of the edge e1 between A and B.

More precisely, I want to efficiently retrieve pairs (prop1, numberD) where prop1 is the property of edge between A -> B (if the edge has this property), and numberD is the property number from D.

I don't know how to efficiently implement this query.

It is easy to retrieve only vertices D (using GremlinPipes):

pipe
.start(startVertex)
.outE("e1")
.inV().hasProperty("number")
.inE("e2")
.outV().hasProperty("id")
.outE("e3")
.inV().hasProperty("number");

But problems occur when I need to get also edges e1 and match them with vertices D. I tried to compute all these steps separately, but is seems to be very inefficient.

Do you have any suggestions how to implement this (maybe using several queries) using gremlin-java or gremlin-groovy? Thanks!

回答1:

Take a look at the Pattern Match Pattern described here:

https://github.com/tinkerpop/gremlin/wiki/Pattern-Match-Pattern

startVertex.outE('e1').as('e')
.inV().hasProperty('number').inE("e2")
.outV().hasProperty("id")
.outE("e3")
.inV().hasProperty("number").as('d')
.table(t)

This should give an iterator of maps

[e:e1, d:D]

From each of these maps, you can easily extract the properties you are interested in.



标签: gremlin titan