在Azure Cosmos DB Graph API中添加或获取顶点 [英] Add or get vertex in Azure Cosmos DB Graph API

查看:67
本文介绍了在Azure Cosmos DB Graph API中添加或获取顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Gremlin, I can create a vertex in an Azure Cosmos DB graph by issuing

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

,然后使用

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

但是,我还没有找到一种发出查询的方法,该查询将在缺少该节点时插入该节点,如果已经存在则仅获取对该节点的引用.有办法吗?

However, I haven't found a way to issue a query that will insert the node if it is missing, and just get the reference to it if it already exists. Is there a way?

我的具体用例是,我想在两个节点之间添加一条边,而不管这些节点(或该边)是否已经存在于单个查询中.我尝试了此更新方法,但显然Cosmos DB不支持Groovy闭包,因此将无法正常工作.

My concrete use case is that I want to add an edge between two nodes, regardless of whether those nodes (or the edge, for that matter) exist already or not, in a single query. I tried this upsert approach, but apparently Cosmos DB does not support Groovy closures, so it won't work.

推荐答案

此时,"upsert模式"的定义和接受程度相对较高.在此处中进行了描述.如果您想扩展它以添加一条边缘,那也是可能的:

The "upsert pattern" is relatively well defined and accepted at this point. It is described here. If you want to extend that to also add an edge, that's possible too:

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'))

如果这看起来有点复杂,那么您绝对可以使用克里姆林宫DSL (尽管我目前不确定CosmosDB是否支持Gremlin字节码).这是一个博客文章中对此进行了讨论.更多细节.

If that looks a bit complex you can definitely simplify with a Gremlin DSL (though I'm not sure that CosmosDB supports Gremlin bytecode at this point). Here's an example with even more complex upsert logic simplified by a DSL. It's discussed in this blog post in more detail.

这篇关于在Azure Cosmos DB Graph API中添加或获取顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆