apache / incubator-hugegraph

A graph database that supports more than 100+ billion data, high performance and scalability (Include OLTP Engine & REST-API & Backends)
https://hugegraph.apache.org
Apache License 2.0
2.63k stars 517 forks source link

[Question] 创建顶点的时候,使用.ifNotExist()判断,但是还是出现下面错误 #2551

Closed liming1010 closed 3 months ago

liming1010 commented 3 months ago

Problem Type (问题类型)

server status (启动/运行异常)

Before submit

Environment (环境信息)

Your Question (问题描述)

异常信息

Caused by: class org.apache.hugegraph.exception.ExistedException: The vertex label '**' has existed
        at org.apache.hugegraph.exception.ServerException.fromResponse(ServerException.java:45)
        at org.apache.hugegraph.client.RestClient.checkStatus(RestClient.java:93)
        at org.apache.hugegraph.rest.AbstractRestClient.post(AbstractRestClient.java:314)
        at org.apache.hugegraph.rest.AbstractRestClient.post(AbstractRestClient.java:250)
        at org.apache.hugegraph.api.schema.VertexLabelAPI.create(VertexLabelAPI.java:46)
        at org.apache.hugegraph.driver.SchemaManager.addVertexLabel(SchemaManager.java:151)
        at org.apache.hugegraph.structure.schema.VertexLabel$BuilderImpl.create(VertexLabel.java:137)
        at org.apache.hugegraph.structure.schema.VertexLabel$BuilderImpl.create(VertexLabel.java:120)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
        ... 11 more

代码

 schema.vertexLabel(value.getEdag())
                    .properties(
                            "**"
                        )
                    .primaryKeys(value.getEdag()).ifNotExist().create();

为什么ifNotExist没有效果

Vertex/Edge example (问题点 / 边数据举例)

No response

Schema [VertexLabel, EdgeLabel, IndexLabel] (元数据结构)

No response

dosubot[bot] commented 3 months ago

.ifNotExist() 方法在创建顶点时没有效果,可能是因为在 hugegraph.properties 文件中配置了 vertex.check_customized_id_exist 选项。这个选项会在添加顶点之前检查是否存在具有相同自定义 ID 的顶点。如果存在相同 ID 的顶点,即使使用了 .ifNotExist(),也会抛出 ExistedException 异常 [1].

你可以检查 hugegraph.properties 文件中的 vertex.check_customized_id_exist 配置项。如果该选项启用,可以尝试禁用它来避免此问题。

此外,确保在创建顶点之前手动检查顶点是否存在也是一种推荐的做法。这可以通过查询顶点来实现,以避免在插入时遇到冲突。

// Example: Check if vertex exists before creating
if (!vertexExists(value.getEdag())) {
    schema.vertexLabel(value.getEdag())
          .properties("**")
          .primaryKeys(value.getEdag())
          .ifNotExist()
          .create();
}

private boolean vertexExists(String vertexId) {
    // Implement the logic to check if the vertex exists
    // This can be a query to the database to see if the vertex with the given ID exists
}

这种方法可以确保在创建顶点之前进行存在性检查,从而避免 ExistedException 异常。

To continue talking to Dosu, mention @dosu.