【问题标题】:Gremlin 3 - get all incoming and outgoing vertices, including their edges and directions and including vertices without edgesGremlin 3 - 获取所有传入和传出顶点,包括它们的边和方向,包括没有边的顶点
【发布时间】:2018-06-22 22:34:45
【问题描述】:

我正在尝试编写一个查询来获取所有传入和传出的顶点,包括它们的边和方向,但是这也会返回那些没有边的顶点。

我现在可以通过强制所有内容至少有一个优势来解决这个问题,但这是我想避免的。

也许值得注意的是,我使用了 Azure CosmosDB 的 Graph API:https://docs.microsoft.com/en-us/azure/cosmos-db/gremlin-support

这是我用来返回所有顶点及其边和相关顶点的查询:

g.V().hasLabel('User').as('User').bothE().as('Edge').otherV().as('RelatedObject').path()

我来自:Gremlin get all incoming and outgoing vertex, including their edges and directions

此查询产生的结果我稍后可以在 C# 应用程序中轻松解析,但是此查询不返回没有边的顶点。

有什么想法吗?

编辑

我得到的最接近的是:

g.V().hasLabel("User").as("User").map(bothE().otherV().fold()).as("RelatedObjects").select("User", "RelatedObjects")

但是,这种方法不会显示UserRelatedObjects 之间的边缘。我还需要边缘能够正确地将这些相关对象映射到父对象。

【问题讨论】:

    标签: azure-cosmosdb graph-databases gremlin tinkerpop3


    【解决方案1】:

    我认为您可以删除所有步骤标签和使用副作用 - 只需使用 project()

    gremlin> g.V().hasLabel('person').
    ......1>   project('user','edges','relatedVertices').
    ......2>     by().
    ......3>     by(bothE().fold()).
    ......4>     by(both().fold())
    ==>[user:v[1],edges:[e[9][1-created->3],e[7][1-knows->2],e[8][1-knows->4]],relatedVertices:[v[3],v[2],v[4]]]
    ==>[user:v[2],edges:[e[7][1-knows->2]],relatedVertices:[v[1]]]
    ==>[user:v[4],edges:[e[10][4-created->5],e[11][4-created->3],e[8][1-knows->4]],relatedVertices:[v[5],v[3],v[1]]]
    ==>[user:v[6],edges:[e[12][6-created->3]],relatedVertices:[v[3]]]
    

    【讨论】:

      【解决方案2】:

      我没有你要测试的图表,但我认为这个一般模式应该适合你。

      g.withSideEffect('x', [] as Set).                        
           V().hasLabel("user").store('x').                                    
           bothE().store('x').                                  
           otherV().store('x').                                 
           cap('x')
      

      修改为不使用 Set

      g.withSideEffect('x', []).                        
           V().has('code',"AUS").store('x').                                    
           bothE().store('x').                                  
           otherV().store('x').                                 
           cap('x').unfold().dedup().fold() 
      

      【讨论】:

      • 似乎不适用于 Microsoft.Azure.Graphs:Gremlin 查询编译错误:脚本编译错误:不支持的 groovy 语言规则:“类型”文本:“设置”@ 第 1 行第 29 列。我认为withSideEffect 也不支持。
      • 那太糟糕了。我在笔记本电脑上使用我自己的图表在 3.3 级别的 TinkerGraph/Gremlin 控制台上运行它,它完成了这项工作。
      • 如果碰巧支持withSideEffect,您可以只使用列表而不是 Set `g.withSideEffect('x', [])。 V().has('code',"AUS").store('x')。两者E().store('x')。其他V().store('x')。帽('x')`
      • 不幸的是,这是我在第一个失败后尝试的第一件事......但我必须给你 +1 的努力。
      【解决方案3】:

      试试这个

      gV().hasLabel("User").as("User").bothE().as("edges").map(select("edges").inV().fold())。 as("RelatedObjects").select("User", "RelatedObjects","edges")

      【讨论】:

      • 有趣的方法,但是它只显示每个用户的一个优势。我尝试在.bothE() 步骤之后添加.fold() 步骤,但这导致 Microsoft.Azure.Graphs 库中出现异常。
      • 实际上它可以工作(它只为每个边缘显示一个User,但它已经足够好了)!
      • 对不起,它实际上仍然忽略了没有边的顶点......我试过这个:.as('User').bothE().as('Edge').map(select('Edge').otherV()).as('RelatedVertex').select('Vertex', 'Edge', 'RelatedVertex')。一定是太累了。
      【解决方案4】:

      这就是 CosmosDB 的工作方式:

      g.V().hasLabel("User")
          .as('Vertex') <------------------- alias for all users
          .map(bothE().fold())
          .as('Edges')
          .map(select('Vertex') <----------- this was the key. Map from the first step
              .bothE().otherV().fold())
          .as('RelatedVertices')
          .select('User', 'Edges', 'RelatedVertices')
      

      很遗憾,Microsoft.Azure.Graphs 库似乎不支持withSideEffect,因此无法使用Kelvin 提出的看起来更优雅的以下方式:

      g.withSideEffect('x', [] as Set).                        
           V().hasLabel("user").store('x').                                    
           bothE().store('x').                                  
           otherV().store('x').                                 
           cap('x')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-26
        • 2017-03-02
        • 2015-12-13
        • 1970-01-01
        • 2019-05-14
        • 2021-11-24
        • 2012-08-13
        • 2017-01-01
        相关资源
        最近更新 更多