尝试删除 Cosmos DB Java SDK 中不存在的文档时不要抛出 DocumentClientException
Do not throw DocumentClientException while trying to delete not existing document in Cosmos DB Java SDK
我正在使用 SQL Api 开发 Azure Cosmos DB。我正在使用来自:
的 Azure SDK
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.7</version>
</dependency>
为了从集合中删除项目(文档),我正在使用:
String documentLink = collectionLink + "/docs/" + documentId;
RequestOptions options = new RequestOptions();
options.setPartitionKey(new PartitionKey(String.valueOf(documentId)));
documentClient.deleteDocument(documentLink, options);
当所需的文档存在时,这段代码就可以正常工作。当带有 documentId 的文档不存在时,我得到一个异常:
com.microsoft.azure.documentdb.DocumentClientException: Entity with the specified id does not exist in the system.
有没有办法删除文档"silently" - 意思是当文档不存在时不会抛出异常?
试图删除一个不存在的项目肯定是 illegal.Such 请求在 cosmos db 中是不能容忍的(没有像 DeleteIfExist
这样的方法)。您还可以从 Cosmos DB REST API.
中找到 404 http 状态代码
所以,你必须捕获这个异常使用Try-Catch
或Throw
来处理它。
try {
RequestOptions options = new RequestOptions();
options.setPartitionKey(new PartitionKey(DOCUMENT_ID));
documentClient.deleteDocument(documentLink, options);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
}
根据@Jay Gong 的回答,我最终设计了我的代码:
try {
RequestOptions options = new RequestOptions();
options.setPartitionKey(new PartitionKey(id);
documentClient.deleteDocument(documentLink(id), options);
} catch (DocumentClientException dce) {
String code = dce.getError().getCode();
if ("NotFound".equals(code)) {
log.warn("Problem while deleting document with id [{}] from Azure Cosmos. error: [{}]", id, dce.getError());
}
else { //handle any other document exception code
}
}
我正在使用 SQL Api 开发 Azure Cosmos DB。我正在使用来自:
的 Azure SDK<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.7</version>
</dependency>
为了从集合中删除项目(文档),我正在使用:
String documentLink = collectionLink + "/docs/" + documentId;
RequestOptions options = new RequestOptions();
options.setPartitionKey(new PartitionKey(String.valueOf(documentId)));
documentClient.deleteDocument(documentLink, options);
当所需的文档存在时,这段代码就可以正常工作。当带有 documentId 的文档不存在时,我得到一个异常:
com.microsoft.azure.documentdb.DocumentClientException: Entity with the specified id does not exist in the system.
有没有办法删除文档"silently" - 意思是当文档不存在时不会抛出异常?
试图删除一个不存在的项目肯定是 illegal.Such 请求在 cosmos db 中是不能容忍的(没有像 DeleteIfExist
这样的方法)。您还可以从 Cosmos DB REST API.
所以,你必须捕获这个异常使用Try-Catch
或Throw
来处理它。
try {
RequestOptions options = new RequestOptions();
options.setPartitionKey(new PartitionKey(DOCUMENT_ID));
documentClient.deleteDocument(documentLink, options);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
}
根据@Jay Gong 的回答,我最终设计了我的代码:
try {
RequestOptions options = new RequestOptions();
options.setPartitionKey(new PartitionKey(id);
documentClient.deleteDocument(documentLink(id), options);
} catch (DocumentClientException dce) {
String code = dce.getError().getCode();
if ("NotFound".equals(code)) {
log.warn("Problem while deleting document with id [{}] from Azure Cosmos. error: [{}]", id, dce.getError());
}
else { //handle any other document exception code
}
}