【问题标题】:Add or get vertex in Azure Cosmos DB Graph API在 Azure Cosmos DB Graph API 中添加或获取顶点
【发布时间】:2018-10-25 11:29:07
【问题描述】:

使用 Gremlin,我可以通过发布在 Azure Cosmos DB 图中创建一个顶点

g.addV('the-label').property('id', 'the-id')

然后使用

找到它
g.V('the-label').has('id', 'the-id')

但是,我还没有找到一种方法来发出查询,该查询将在节点丢失时插入节点,如果节点已存在则仅获取对它的引用。有什么办法吗?


我的具体用例是我想在两个节点之间添加一条边,无论这些节点(或边,就此而言)是否已经存在,在单个查询中。我试过this upsert approach,但显然 Cosmos DB 不支持 Groovy 闭包,所以它不起作用。

【问题讨论】:

    标签: azure-cosmosdb gremlin


    【解决方案1】:

    “upsert 模式”在这一点上得到了相对明确的定义和接受。它被描述为here。如果您想扩展它以添加边缘,这也是可能的:

    g.V().has('event','id','1').
      fold().
      coalesce(unfold(),
               addV('event').property('id','1')).as('start').
      coalesce(outE('link').has('id','3'),
               coalesce(V().has('event','id','2'), 
                        addV('event').property('id','2')).
                        addE('link').from('start').property('id','3'))
    

    如果这看起来有点复杂,您绝对可以使用Gremlin DSL 进行简化(尽管我目前不确定 CosmosDB 是否支持 Gremlin 字节码)。这是一个example,它具有由 DSL 简化的更复杂的 upsert 逻辑。在blog post 中有更详细的讨论。

    【讨论】:

    • 当前 Cosmos 不支持字节码,所以没有 DSL,但有人告诉我他们正在研究它
    【解决方案2】:

    请看这个。

    http://tinkerpop.apache.org/docs/current/reference/#coalesce-step

    你可以试试

    g.Inject(0).coalesce(__.V().has('id', 'the-id'), addV('the-label').property('id', 'the-id' '))

    顺便说一句,您将无法使用 g.V('the-label').has('id', 'the-id') 找到顶点。

    g.V() 接受顶点 id 作为参数而不是顶点标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 2021-11-08
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      相关资源
      最近更新 更多