【问题标题】:hasNext returning wrong resulthasNext 返回错误结果
【发布时间】:2020-09-02 14:45:54
【问题描述】:

我想检查两个节点之间的关系是否存在。如果存在,我想更新属性,否则在指定节点之间添加新关系。一个 groovy 脚本用于从 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')

g.V().has('label_A','A').outE('to').inV().has('label_B','B').hasNext() 即使通过 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')

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

【问题讨论】:

  • 几个建议和问题: 1.您可以将 outE('to').inV() 替换为 out('to') 2.您是将 label_A 存储为属性还是顶点标签?如果它是一个顶点标签,那么您的查询可能不正确。如果是顶点标签,您可以将 has('label_A','A') 替换为 hasLabel('A') (b 相同)
  • label_A 不是顶点标签。它是一种财产。由于标签是保留的,我使用的是 label_A。我正在使用 outE('to').inV() 因为可能有很多边缘从 A 到 B、C 等。但我想选择 label_B 的边缘,而不是其他边缘

标签: graph gremlin janusgraph


【解决方案1】:

从纯粹的 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 控制台会话中看到它的实际效果:

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','existed'),
......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,label:vertex],created:newAdded]
gremlin> g.V().has('label_A','A').
......1>   outE('to').where(inV().has('label_B','B')).
......2>   fold().
......3>   coalesce(unfold().property('created','existed'),
......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,label:vertex],created:existed]

至于为什么您的方法在 JanusGraph 中不起作用,鉴于所提供的信息很难说。也许您应该尝试使用 JanusGraph 自己重新创建我的 Gremlin 控制台会话,看看会发生什么。如果它仍然不适合您,您可以提供一个完全失败的示例供 JanusGraph 专家查看。

【讨论】:

  • 在我的情况下不起作用。注意:我正在使用一个 groovy 脚本,它可以批量加载 CSV 记录并执行查询。有没有其他办法?
  • 问题是边缘没有被选中,这是导致问题的原因
  • 您能否更具体地说明什么不起作用?你是说我与样本数据的精确 gremlin 会话不适用于 JanusGraph 吗?如果您说,您只是将我的查询粘贴到您的 groovy 脚本中并且它不起作用,那么您的脚本可能有问题,在这种情况下我们可能需要查看它。在最坏的情况下,您可能需要提供可以在 Gremlin 控制台中执行的完全可重现的故障。请先用 JanusGraph 重新创建我的会话,看看是否可行。该测试的结果将告诉我们很多。
  • 我正在使用一个多处理 groovy 脚本,它分批执行 CSV 文件。问题是查询在控制台上执行时返回所需的输出,但在使用多处理 groovy 脚本执行时返回错误的结果。例如,考虑查询 g.V().has('label_A','A').outE('to').inV().has('label_B','B').hasNext() 在控制台中返回 true , 但通过脚本为 False。 has 语句中的“A”和“B”是通过 CSV 文件获取的。例如 g.V().has('label_A',record[0]).outE('to').inV().has('label_B',record[1]).hasNext() record[0] 包含 'A ' anf 记录 [1] 包含 'B'。个人聊天?
  • 你的 groovy 脚本有问题 - 你的遍历没有执行。你需要iterate()你的遍历 - tinkerpop.apache.org/docs/current/tutorials/the-gremlin-console/…
猜你喜欢
  • 2021-05-05
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多