Azure documentDB - 已存在具有指定标识或名称的资源

时间:2018-03-27 14:32:40

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

在我的应用程序中的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 / 131662740073245648p,RequestStats :,SDK:Microsoft .Azure.Documents.Common / 1.21.0.0,StatusCode:Conflict 2018-03-27 14:42:058错误c.h.m.a.e.GlobalExceptionHandler - 异常:class org.apache.catalina.connector.ClientAbortException消息:java.io.IOException:远程主机强制关闭现有连接 2018-03-27 14:42:058 WARN oswsmmaExceptionHandlerExceptionResolver - 无法调用@ExceptionHandler方法:public 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)     at 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)     at 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 fruites to Customer Reference : " + fruitesModel.getId() + " " + e .getMessage()); }

更新功能

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

创建文档时遇到问题。

2 个答案:

答案 0 :(得分:1)

使用 DocumentClient.UpsertDocumentAsync 代替documentClient.replaceDocumentAsync

Upsert将创建一个文档,如果它尚未存在,否则会覆盖它。替换要求文档已存在,然后覆盖它。使用哪个是你的应用程序的一个问题。

答案 1 :(得分:1)

如果您正在使用 ADF,则在 Sink 中使用 upsert,而您可以使用目标集合进行左外连接源,重要的是明确地向右或向左选择源,而不是让 ADF 选择它并检查 id 或任何唯一列在 Alter Row 的 upsert 条件下为 null

data flow steps

相关问题