根据另一个更新顶点属性

时间:2019-01-10 19:31:58

标签: gremlin tinkerpop amazon-neptune

我想根据递增的用户highScore更新score。如果新增加的score大于highScore,则设置highScore = score

// initial data
g.addV("player")
  .property(id, 1)
  .property(single, "score", 0)
  .property(single, "highScore", 0)

// increment score by 1 and set highScore if required
g.V(1)
  .sack(assign)
  .by("score")
  .sack(sum)
  .by(__.constant(1))
  .property(single, "score", sack())
  .choose(
    __.values("highScore").is(lt(__.values("score"))),
    __.property(single, "highScore", __.values("score")))
  )

lt(__.values("score"))上似乎出错。将其解析为遍历而不是值。

  

com.amazon.neptune.tinkerpop.structure.NeptuneGraph $ NeptuneGraphTraversal   无法转换为java.lang.Integer

如何将score的当前值传递给该谓词?我尝试添加.value().iterate().next()

1 个答案:

答案 0 :(得分:1)

这种使用where()的方法似乎可行:

gremlin> g.V(1).as('a').
......1>   sack(assign).
......2>     by("score").
......3>   sack(sum).
......4>     by(__.constant(1)).
......5>   property(single, "score", sack()).
......6>   choose(where('a', lt('a')).by('highScore').by('score'),
......7>          __.property(single, "highScore", sack()))
==>v[1]
gremlin> g.V().valueMap()
==>[score:[1],highScore:[1]]
gremlin> g.V(1).as('a').
......1>   sack(assign).
......2>     by("score").
......3>   sack(sum).
......4>     by(__.constant(1)).
......5>   property(single, "score", sack()).
......6>   choose(where('a', lt('a')).by('highScore').by('score'),
......7>          __.property(single, "highScore", sack()))
==>v[1]
gremlin> g.V().valueMap()
==>[score:[2],highScore:[2]]
gremlin> g.V().property('highScore',10)
==>v[1]
gremlin> g.V().valueMap()
==>[score:[2],highScore:[10]]
gremlin> g.V(1).as('a').
......1>   sack(assign).
......2>     by("score").
......3>   sack(sum).
......4>     by(__.constant(1)).
......5>   property(single, "score", sack()).
......6>   choose(where('a', lt('a')).by('highScore').by('score'),
......7>          __.property(single, "highScore", sack()))
==>v[1]
gremlin> g.V().valueMap()
==>[score:[3],highScore:[10]]