CosmosDB图:"upsert"查询模式 [英] CosmosDB Graph : "upsert" query pattern

查看:84
本文介绍了CosmosDB图:"upsert"查询模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Gremlin查询语言的新手. 我必须在Cosmos DB图形上插入数据(使用Gremlin.Net包),无论图形中是否已存在顶点(或边).如果数据存在,我只需要更新属性. 我想使用这种模式:

I am new to Gremlin query language. I have to insert data on a Cosmos DB graph (using Gremlin.Net package), whether the Vertex (or Edge) already exists in the graph or not. If the data exists, I only need to update the properties. I wanted to use this kind of pattern:

g.V().hasLabel('event').has('id','1').tryNext().orElseGet {g.addV('event').has('id','1')}

但是Gremlin.Net/Cosmos DB图形API不支持它.有没有一种方法可以在单个查询中进行向上查询?

But it is not supported by Gremlin.Net / Cosmos DB graph API. Is there a way to make a kind of upsert query in a single query ?

谢谢.

推荐答案

有很多方法可以做到这一点,但我认为TinkerPop社区通常对此方法有所了解:

There are a number of ways to do this but I think that the TinkerPop community has generally settled on this approach:

g.V().has('event','id','1').
  fold().
  coalesce(unfold(),
           addV('event').property('id','1'))

基本上,它将使用has()查找事件",并使用fold()步骤强制转换为列表.该列表将为空或其中包含Vertex.然后使用coalesce(),它尝试对unfold()进行列表,如果它具有立即返回的Vertex,则执行addV().

Basically, it looks for the "event" with has() and uses fold() step to coerce to a list. The list will either be empty or have a Vertex in it. Then with coalesce(), it tries to unfold() the list and if it has a Vertex that is immediately returned otherwise, it does the addV().

如果要在找到元素后更新现有属性,只需在coalesce()之后添加property()步骤:

If the idea is to update existing properties if the element is found, just add property() steps after the coalesce():

g.V().has('event','id','1').
  fold().
  coalesce(unfold(),
           addV('event').property('id','1')).
  property('description','This is an event')

如果您需要知道返回的顶点是否为新",则可以执行以下操作:

If you need to know if the vertex returned was "new" or not then you could do something like this:

g.V().has('event','id','1').
  fold().
  coalesce(unfold().
           project('vertex','exists').
             by(identity()).
             by(constant(true)),
           addV('event').property('id','1').
           project('vertex','exists').
             by(identity()).
             by(constant(false)))

可以在以下问题上找到有关此主题的其他阅读:"为什么需要使用合并来折叠/展开条件插入?"

Additional reading on this topic can be found on this question: "Why do you need to fold/unfold using coalesce for a conditional insert?"

还请注意,此处介绍了可选的边插入:"如果没有使用gremlin,则添加边".

Also note that optional edge insertion is described here: "Add edge if not exist using gremlin".

最后一点是,尽管有人问到有关CosmosDB的问题,但答案通常适用于所有启用了TinkerPop的图形.当然,图如何优化此Gremlin是一个单独的问题.如果图具有本机上载功能,则该功能可能会或可能不会在此Gremlin的幕后使用,因此,可能会有更好的方法通过图系统本机API来实现上装(当然,选择该路径会降低可移植性的可移植性)您的代码).

As a final note, while this question was asked regarding CosmosDB, the answer generally applies to all TinkerPop-enabled graphs. Of course, how a graph optimizes this Gremlin is a separate question. If a graph has native upsert capabilities, that capability may or may not be used behind the scenes of this Gremlin so there may be better ways to implement upsert by way of the graphs systems native API (of course, choosing that path reduces the portability of your code).

这篇关于CosmosDB图:"upsert"查询模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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