微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

hasNext返回错误的结果

如何解决hasNext返回错误的结果

我想检查两个节点之间的关系是否存在。如果存在,我想更新属性,否则在指定节点之间添加新关系。常规脚本用于从CSV文件读取数据并运行查询

g.V().has('label_A','A').outE('to').inV().has('label_B','B').hasNext() ? g.V().has('label_A','A').outE('to').as('e').inV().has('label_B','B').select('e').property('created','existed') : g.V().has('label_A','A').as('fromV').V().has('label_B','B').as('toV').addE('to').from('fromV').to('toV').property('created','newAdded')

gV()。has('label_A','A')。outE('to')。inV()。has('label_B','B')。hasNext()始终返回false通过groovy脚本运行时,给定的两个节点之间存在关系。 gremlin控制台上的同一命令将返回预期的输出。因此,总是会创建新的关系。

也尝试了以下查询

g.V().hasLabel('label_A','A').as('v').V().has('label_B','B').coalesce(__.inE('to').where(outV().as('v')),addE('to').from('v').property('created','newAdded')).property('created','existed')

以上查询无效。没有添加任何关系。

解决方法

从纯Gremlin的角度来看,我认为您最好将遍历写为:

g.V().has('label_A','A').
  outE('to').where(inV().has('label_B','B')).
  fold().
  coalesce(unfold().property('created','existed'),addE('to').
           from(V().has('label_A','A')).
           to(V().has('label_B','B')).
           property('created','newAdded'))

这样,它将在单个请求/事务中执行,而不是在两个单独的操作中执行。您可以在下面的TinkerGraph的Gremlin Console会话中看到它的运行情况:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0],standard]
gremlin> g.addV().property('label_A','A').iterate()
gremlin> g.addV().property('label_B','B').iterate()
gremlin> g.V().has('label_A','A').
......1>   outE('to').where(inV().has('label_B','B')).
......2>   fold().
......3>   coalesce(unfold().property('created',......4>            addE('to').
......5>            from(V().has('label_A','A')).
......6>            to(V().has('label_B','B')).
......7>            property('created','newAdded'))
==>e[4][0-to->2]
gremlin> g.E().elementMap()
==>[id:4,label:to,IN:[id:2,label:vertex],OUT:[id:0,created:newAdded]
gremlin> g.V().has('label_A',created:existed]

关于您的方法为什么在JanusGraph中不起作用的原因,鉴于所提供的信息,很难说。也许您应该尝试使用JanusGraph自己重新创建我的Gremlin Console会话,然后看看会发生什么。如果仍然无法解决问题,则可以为JanusGraph专家提供一个完全失败的示例。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。