1

I am having an edge(say edge) between two vertices(say v1 and v2). And edge direction is from v1 to v2. In my current situation, I have to count the no. of edges are incoming to v2 based on some condition. Now I am able to count the number of edges, but I am not able to retrieve the data of v2.

     g.V().hasLabel('V2').has('type','c').as('p').project('v2Data').by(select('p').inE('edge').count().is(gt(5)).valueMap(true))

By the above approch, I have got an error

java.lang.Long cannot be cast to org.apache.tinkerpop.gremlin.structure.Element

The other approach I tried is,

     g.V().hasLabel('v2').has('type','c').as('p').project('v2Data').by(select('p').inE('edge').count().is(gt(5)).select('p').valueMap(true))

The provided traverser does not map to a value: v[286724240]->[SelectOneStep(last,p), NoOpBarrierStep(2500), JanusGraphVertexStep(IN,[willingToPlayAt],edge), RangeGlobalStep(0,6), CountGlobalStep, IsStep(gt(5)), SelectOneStep(last,p), NoOpBarrierStep(2500), PropertyMapStep(value)]

I have tried this stack overflow answer fold() approach

I want my output as v2data = [{},{}]

reference: g.addV('game').property('id',1).as('1'). addV('game').property('id',2).as('2'). addV('game').property('id',3).as('3'). addV('game').property('id',4).as('4'). addV('game').property('id',5).as('5'). addV('game').property('id',6).as('6'). addV('loc').property('id',p1).as('p1'). addV('loc').property('id',p2).as('p2'). addE('edge').from('1').to('p1'). addE('edge').from('2').to('p1'). addE('edge').from('3').to('p1'). addE('edge').from('4').to('p1'). addE('edge').from('5').to('p1'). addE('edge').from('6').to('p1'). addE('edge').from('1').to('p2'). addE('edge').from('2').to('p2');

query:

g.V().hasLabel('loc').has('type','c').
  project('locData').
  by(where(inE('edge').count().is(gt(5))).valueMap(true).fold())

Expected output:

{locData=[{id=[p1],....}]}

because gt(5) is satisfied only for loc with id=p1.

But I am getting my result as

{locData=[{id=[p1],....}]}
{locData=[]}(p2 is not satisfied with my condition.)
Himabindu
  • 634
  • 8
  • 22

1 Answers1

1

I'm not completely clear what you're trying to do, but that error is easy to explain. you are calling count() which is a reducing step that produces a Long. You then try to call valueMap() on that Long which produces an error, since valueMap() can only be called on an Element (e.g. Vertex).

So, if I take your query as is, it should start returning something if you do:

g.V().hasLabel('V2').has('type','c').as('p').
  project('v2Data').
    by(select('p').where(inE('edge').count().is(gt(5))).valueMap(true).fold())

You need the fold() so that if your condition is not met, you get something returned if your filter returns no values (i.e. fold() will reduce to an empty list in that case).

That said, your query still could use some work. First, I don't see the need for "p" because its value is the current traverse in the stream, so:

g.V().hasLabel('V2').has('type','c').
  project('v2Data').
    by(where(inE('edge').count().is(gt(5))).valueMap(true).fold())

Then, I prefer combination of the two has() steps:

g.V().has('V2','type','c').
  project('v2Data').
    by(where(inE('edge').count().is(gt(5))).valueMap(true).fold())

Finally, you don't really need to count() all the edges to find out there are more than "5", you just need a max of "6" so maybe:

g.V().has('V2','type','c').
  project('v2Data').
    by(where(inE('edge').limit(6).count().is(gt(5))).valueMap(true).fold())
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • ,I have tried this approach `g.V().hasLabel('V2').has('type','c').project('v2Data').by(where(inE('edge').count().is(gt(5))).valueMap(true).fold())`. Because of fold() I am getting my output as **==>{v2Data[{id:id1,name:name1}]} ==>{v2Data=[]}==>{v2Data=[]}** – Himabindu Jun 14 '19 at 06:09
  • If my condition is true I am getting some data which is ok. But I don't want to get empty arrays when the condition is failed. So can you suggest something other than fold()? – Himabindu Jun 14 '19 at 06:15
  • well, what do you want instead of an empty array? or do you just not what that vertex represented in the first place? I think that if you want a more exact answer you should edit your question to include sample data as shown here: https://stackoverflow.com/questions/51388315/gremlin-choose-one-item-at-random and then some expected output. – stephen mallette Jun 14 '19 at 10:40