Warm tip: This article is reproduced from serverfault.com, please click

tinkerpop-ID已存在的边缘

(tinkerpop - Edge with id already exists)

发布于 2020-12-01 08:05:21

我正在使用以下内容使用Gremlin提升优势。我遵循这里提到的食谱这是我正在使用的代码。该代码在lambda中运行,该lambda与Amazon Neptune中托管的群集进行通信

    public void createEdge(final String id, final String label, final String fromId, final String toId, final Map<String, String> properties) {
        this.graphTraversalSource
            .V(fromId) // get vertex of id given for the source
            .as("fromVertex") // label as fromVertex to be accessed later
            .V(toId) // get  vertex of id given for destination
            .coalesce( // evaluates the provided traversals in order and returns the first traversal that emits at least one element
                inE(label) // check incoming edge of label given
                    .has(T.id, id) // also check for incoming edge of given ID
                    .where( // conditional check to check if edge exists
                        outV() // get destination vertex of the edge to check
                            .as("fromVertex")), // against staged vertex
                addE(label) // add edge if not present
                    .property(T.id, id) // with given id
                    .from("fromVertex")) // from source vertexx
            .next(); // end traversal to commit to graph

        log.info("Created edge {} if it doesn't exists between {} and {}", id, fromId, toId);
    }

与tinkerpop网站中提到的示例食谱的唯一区别是我添加的这一步骤。就我而言,我想在具有相同标签但ID不同的相同顶点之间创建多个边

.has(T.id, id) // also check for incoming edge of given ID

但是我收到以下异常

Caused by: java.util.concurrent.CompletionException: org.apache.tinkerpop.gremlin.driver.exception.ResponseException: 
{
    "detailedMessage": "Edge with id already exists: 123",
    "requestId": "dce46db8-1d0a-4717-a412-ee831973b177",
    "code": "ConstraintViolationException"
}

但是,当我在gremlin控制台中运行相同的查询时,实验成功。以下查询(我执行了几次)返回相同的边。但是远程gremlin服务器不会发生同样的情况

g.V().has('product','id','C1').as('v1').V().has('product','id','C2').coalesce(__.inE('rel').has('id', 'E1').where(outV().as('v1')), addE('rel').property('id', 'E1').from('v1')).next()

有人可以帮我解决这个错误

谢谢

Questioner
CHID
Viewed
1
Taylor Riggan 2020-12-01 21:52:39

上面具有的查询(Lambda函数代码)与问题末尾引用的查询有一个主要区别。在问题末尾的查询中:

g.V().has('product','id','C1').as('v1').
   V().has('product','id','C2').
      coalesce(
          __.inE('rel').has('id', 'E1').where(outV().as('v1')),  
          addE('rel').property('id', 'E1').from('v1')).
   next()

此查询has('id','E1')用于检查边缘的ID,而不是has(T.id,...)has('id'正在寻找名为“ id”的自定义属性,而has(T.id正在寻找实际的边缘ID。

同样适用property('id'如果使用(property('id',则创建该边缘时,Neptune(专门)将为该边缘的T.id值创建一个UUID。因此,你最终将获得许多具有不同T.id但具有相同id属性的边缘

注意:你只需要T.id在Python中编码时使用该符号。如果使用Gremlin控制台,则只需使用has(id,"myId")或即可property(id,"myId")周围没有引号id