admin管理员组

文章数量:1421155

I'm trying to write a Tinkerpop Gremlin query that returns data about each path between a particular root node and all of its leaves. In particular, I'd like to know:

  • details about the leaf node
  • the minimum value of an edge property across all edges in the path between leaf node and root (if there is no such value along a path, report null)
  • how many edges are in the path

Ideally this data would be grouped by the name of the leaf node, as doing so allows me to paginate the results by leaf node.

I've managed to write a query that works well enough against the tinkerpop/gremlin-server:3.6.5 Docker container (the result is not nearly as succinct as would be ideal, but it was something I could at least work with). My problem is that I'm ultimately writing this query to run against an AWS Neptune instance, and the query does not work when running against Neptune. I'm quite new to Gremlin and was hoping someone might be able to assist me in updating this query so that it works against Neptune (and making the result more succinct would be an awesome added bonus).

The following diagram is a basic example: picture of example graph

In this example, an ideal result would be something similar to:

==> x1: {"leafNode": {result of running elementMap on the leaf node x1}, "paths": [{"edgeCount": 1, "minVal": null}]}
==> x2: {"leafNode": {result of running elementMap on the leaf node x2}, "paths": [{"edgeCount": 1, "minVal": 20}]}
==> x3: {"leafNode": {result of running elementMap on the leaf node x3}, "paths": [{"edgeCount": 3, "minVal": 17}, {"edgeCount": 2, "minVal": 16}]}

The query that I've written is (assuming 4 is the ID of the root node):

g.V(4).repeat(__.inE('member').outV()).until(hasLabel('X')).emit().filter(hasLabel('X')).order().by('name').
  path().
  project('name', 'leafNode', 'minEVal', 'edgeCount').
    by(unfold().where(label().is('X')).values('name')).
    by(unfold().where(label().is('X')).elementMap()).
    by(unfold().where(label().is('member')).values('e').fold().coalesce(unfold().min(), constant(null))).
    by(unfold().where(label().is('member')).count()).
  group().by('name').unfold()

which yields, for the example diagram above:

==>X1=[{name=X1, leafNode={id=1, label=X, name=X1}, minEVal=null, edgeCount=1}]
==>X2=[{name=X2, leafNode={id=2, label=X, name=X2}, minEVal=20, edgeCount=1}]
==>X3=[{name=X3, leafNode={id=3, label=X, name=X3}, minEVal=16, edgeCount=2}, {name=X3, leafNode={id=3, label=X, name=X3}, minEVal=17, edgeCount=3}]

As I mentioned above, this result is a bit repetitive because it duplicates the leaf node details for leaf nodes that have multiple paths between themselves and the root node, but it was workable.

AWS Neptune does not like the last line group().by('name').unfold() and returns the error:

"code":"UnsupportedOperationException","message":"java.util.LinkedHashMap cannot be cast to .apache.tinkerpop.gremlin.structure.Element"

If I don't include the group by line, the result of the query looks like:

==>[name:X1,leafNode:[id:1,label:X,name:X1],minEVal:null,edgeCount:1]
==>[name:X2,leafNode:[id:2,label:X,name:X2],minEVal:20,edgeCount:1]
==>[name:X3,leafNode:[id:3,label:X,name:X3],minEVal:16,edgeCount:2]
==>[name:X3,leafNode:[id:3,label:X,name:X3],minEVal:17,edgeCount:3]

which complicates performing pagination.

Setup code to replicate the example graph in Gremlin:

g.addV('X').property(id,1).property('name', 'X1').as('X1').
  addV('X').property(id,2).property('name', 'X2').as('X2').
  addV('X').property(id,3).property('name', 'X3').as('X3').
  addV('A').property(id,4).property('name', 'A1').as('A1').
  addV('A').property(id,5).property('name', 'A2').as('A2').
  addV('A').property(id,6).property('name', 'A3').as('A3').
  addV('A').property(id,7).property('name', 'A4').as('A4').
  addE('member').from('X1').to('A1').
  addE('member').property('e', 20).from('X2').to('A1').
  addE('member').property('e', 17).from('X3').to('A3').
  addE('member').property('e', 19).from('X3').to('A4').
  addE('member').from('A2').to('A1').
  addE('member').from('A3').to('A2').
  addE('member').property('e', 16).from('A4').to('A1').
  iterate()

本文标签: Gremlin Query for RoottoLeaf Paths Fails in AWS Neptune How to Fix Grouping IssueStack Overflow