【问题标题】:Get array's first item as object in TinkerPop3 Gremlin query and JanusGraph在 TinkerPop3 Gremlin 查询和 JanusGraph 中获取数组的第一项作为对象
【发布时间】:2019-02-23 20:04:52
【问题描述】:

我在将 gremlin 查询从 v2 迁移到 v3 时遇到了这个问题。

V2-wayinE().has(some condition).outV().map().toList()[0] 将返回一个对象。这包含在transform{label: it./etc/} 步骤中。

V3 方式,仍然是 WIP:inE().has(some condition).outV().fold() 将返回一个数组。这包含在project(...).by(...) 步骤中。

V3 工作正常,我只需要手动从数组中解包一个项目。我想知道是否有更理智的方法(无论如何,这感觉像是对图形不友好的步骤)。

环境:JanusGraph、TinkerPop3+。对于 v2:Titan graph db 和 TinkerPop2+。

更新: V3 查询示例

inE('edge1').
  has('cond1').outV(). // one vertex left
  project('items', 'count'). // pagination
    by(
      order().
        by('field1', decr).
          project('vertex_itself', 'vertex2', 'vertices3').
            by(identity()).
            by(outE('edge2').has('type', 'type1').limit(1).inV().fold()). // now this is empty array or single-element array, can we return element itself?
            by(inE('edge2').has('type', 'type2').outV().fold()).
          fold()).
    by(count())

想要的结果形状:

[{
  items: [
    {vertex_itself: Object, vertex2: Object/null/empty, veroces3: Array},
    {}...
  ],
  cont: Number,
}]

问题: vertex2 属性始终是一个数组,为空或单元素。
预期: vertex2 为对象或空/空。

更新 2: 原来我的查询还没有完成,如果has('cond1').outV() 步骤中没有单个元素,它会返回许多对象,例如[{items, count}, {items, count}...]

【问题讨论】:

  • 为什么project().by(...fold())中有fold()步骤?如果我删除它们,我会收到错误 The provided traverser does not map to a value
  • 现在我看到这个问题非常令人困惑,即使最初给定的查询也没有像我自己描述的那样工作。让我们暂时关闭这个

标签: gremlin tinkerpop tinkerpop3 janusgraph graph-traversal


【解决方案1】:

我可能不完全明白,但听起来是这样的:

inE().has(some condition).outV().fold()

您只想抓取遇到的第一个顶点。如果那是正确的,那么有理由fold() 吗?也许只是这样做:

inE().has(some condition).outV().limit(1)

【讨论】:

  • 好点,我会像这样添加它inE().has(some condition).limit(1).outV()。但是,limit 步骤仍将返回具有单个元素的数组。是否可以获取元素本身或 null,而不是数组?
  • 添加了关于fold()步骤用法的问题评论
【解决方案2】:

看起来您的主要问题是从遍历中获取单个项目。

您可以使用next() 执行此操作,它将检索当前遍历迭代中的下一个元素:

inE().has(some condition).outV().next()

我认为,iteratee 的结构是特定于实现的。例如在 javascript 中,您可以使用 value 属性访问该项目:

const result = await inE().has(some condition).outV().next();
const item = result.value;

【讨论】:

    猜你喜欢
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2023-04-04
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多