【问题标题】:Azure documentDB - Resource with specified id or name already existsAzure documentDB - 具有指定 ID 或名称的资源已存在
【发布时间】:2021-07-30 21:18:48
【问题描述】:

在我的应用程序的 Azure Cosmo DocumentDB 中,出现以下错误

2018-03-27 14:42:057 错误 c.h.m.s.CosmosDBFruiteService - 不能 将 Fruites 添加到客户参考中: 11416e34-3620-45a4-b3be-b845bbf41762
消息:{“错误”:[“具有指定 ID 或名称的资源已存在。”]} ActivityId:1b70b944-d581-4640-8785- 819400433bb4, 请求 URI: /apps/8d13f597-c7e4-4d60-8713-8a0e9abaa9ac/services/ce0d287f-0125-482b-b32c-22108b5b0ed3/partitions/42c80a49-8042-4032-8cfd-9937ecea8dcc/replicas/1316682740046 RequestStats:,SDK:Microsoft.Azure.Documents.Common/1.21.0.0, 状态码:冲突 2018-03-27 14:42:058 错误 c.h.m.a.e.GlobalExceptionHandler - 异常:类 org.apache.catalina.connector.ClientAbortException 消息: java.io.IOException:现有连接被强制关闭 远程主机 2018-03-27 14:42:058 警告 o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - 调用失败 @ExceptionHandler 方法:公共 java.util.Map com.hm.myfeed.api.exception.GlobalExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest,javax.servlet.http.HttpServletRequest) org.apache.catalina.connector.ClientAbortException: java.io.IOException:现有连接被强制关闭 远程主机在 org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:356) 在 org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:815) 在 org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:720) 在 org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:391) 在 org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:369) 在 org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96) 在 org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:639)

我有点不明白这一点。异常发生在首先检查集合是否存在(确实存在)然后是否不创建它的代码中。显然创建会失败,集合存在!!

创建函数

try {
    fruitesDocument = documentClient.createDocument(getCollectionLink(), fruitesDocument , null, false).getResource();
} catch (DocumentClientException e) {
    LOG.error("Could not add fruits for Customer Reference: " + fruitesModel.getId() + "  " + e.getMessage());
}

更新功能:

try {
    fruitesDocument = documentClient.replaceDocument(fruitesDocument , null).getResource();
} catch (DocumentClientException e) {
    LOG.error("Error while updating fruites: " + e.getMessage());
}

创建文档时遇到问题。

【问题讨论】:

    标签: azure azure-cosmosdb document-database azure-java-sdk


    【解决方案1】:

    使用 DocumentClient.UpsertDocumentAsync 代替 documentClient.replaceDocumentAsync

    如果文档尚不存在,Upsert 将创建一个文档,否则将其覆盖。替换要求文档已经存在,然后覆盖它。使用哪个是您的应用程序所关心的问题。

    【讨论】:

    • 感谢 Sajeetharan 的快速响应,但在 createDocument 中出现错误感谢您的建议,我将在 replaceDocument 中更新。
    • Message: {"Errors":["Resource with specified id or name already exists."]} 我用的是java版本,找不到这个方法replaceDocumentAsync
    【解决方案2】:

    如果您使用 ADF,则在 Sink 中使用 upsert,虽然您可以对目标集合进行左外连接源,但重要的是显式选择右侧或左侧的源,而不是允许 ADF 选择它并检查 id 或任何唯一列在 Alter Row 的 upsert 条件下为 null

    【讨论】:

      猜你喜欢
      • 2011-03-29
      • 2020-08-08
      • 2017-09-18
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多