use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SamplePatchQuickstart method patchAddSingleJsonString.
// demonstrates a single patch (add) operation using json String
private void patchAddSingleJsonString(String id, String partitionKey) {
logger.info("Executing Patch with single 'add' operation to add json from String");
String json = "[{\"givenName\": \"Goofy\"},{\"givenName\": \"Shadow\"}]";
ObjectMapper objectMapper = new ObjectMapper();
try {
// Converting json String to JsonNode using Jackson
JsonNode jsonNode = objectMapper.readTree(json);
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
// attribute does not exist, will be added
cosmosPatchOperations.add("/pets", jsonNode);
CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
CosmosItemResponse<Family> response = this.container.patchItem(id, new PartitionKey(partitionKey), cosmosPatchOperations, options, Family.class);
logger.info("Item with ID {} has been patched", response.getItem().getId());
} catch (Exception e) {
logger.error("failed", e);
}
}
use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SamplePatchQuickstart method patchAddSingle.
// demonstrates a single patch (add) operation
private void patchAddSingle(String id, String partitionKey) {
logger.info("Executing Patch with single 'add' operation");
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
// attribute does not exist, will be added
cosmosPatchOperations.add("/vaccinated", false);
CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
try {
CosmosItemResponse<Family> response = this.container.patchItem(id, new PartitionKey(partitionKey), cosmosPatchOperations, options, Family.class);
logger.info("Item with ID {} has been patched", response.getItem().getId());
} catch (Exception e) {
logger.error("failed", e);
}
}
use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SamplePatchQuickstart method patchReplace.
// can be used to change value of an existing attribute. will fail if the
// attribute does not exist
private void patchReplace(String id, String partitionKey) {
logger.info("Executing Patch with 'replace' operations");
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations.replace("/district", "new_replaced_value").replace("/parents/0/familyName", "Andersen");
// add to empty array. works as expected
Child c1 = new Child();
c1.setFamilyName("Andersen");
c1.setFirstName("Selena");
c1.setGender("f");
cosmosPatchOperations.replace("/children", Arrays.asList(c1));
// un-commenting below will result in BadRequestException: add to an existing
// array, with index equal to (or more than) array length
/*
* Child c2 = new Child(); c2.setFamilyName("Andersen");
* c2.setFirstName("John"); c2.setGender("f");
* cosmosPatchOperations.replace("/children/1", c2);
*/
// attribute does not exist. un-commenting below will cause exception.
// cosmosPatchOperations.replace("/does_not_exist", "new value");
CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
try {
CosmosItemResponse<Family> response = this.container.patchItem(id, new PartitionKey(partitionKey), cosmosPatchOperations, options, Family.class);
logger.info("Item with ID {} has been patched", response.getItem().getId());
} catch (Exception e) {
logger.error("failed", e);
}
}
use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SamplePatchQuickstart method patchRemove.
// demonstrates how to remove an attribute, including array
private void patchRemove(String id, String partitionKey) {
logger.info("Executing Patch with 'remove' operations");
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations.remove("/registered").remove("/children/0/pets/0");
// un-commenting this will cause an exception since the attribute does not exist
// cosmosPatchOperations.remove("/attribute_does_not_exist");
CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
try {
CosmosItemResponse<Family> response = this.container.patchItem(id, new PartitionKey(partitionKey), cosmosPatchOperations, options, Family.class);
logger.info("Item with ID {} has been patched", response.getItem().getId());
} catch (Exception e) {
logger.error("failed", e);
}
}
use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SamplePatchQuickstart method patchConditional.
// demonstrates how to use a predicate for conditional update
private void patchConditional(String id, String partitionKey, String predicate) {
logger.info("Executing Conditional Patch with predicate");
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations.add("/vaccinated", false).replace("/district", "new_replaced_value");
CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
logger.info("predicate: {}", predicate);
options.setFilterPredicate(predicate);
// predicate match failure will result in BadRequestException
try {
CosmosItemResponse<Family> response = this.container.patchItem(id, new PartitionKey(partitionKey), cosmosPatchOperations, options, Family.class);
logger.info("Item with ID {} has been patched", response.getItem().getId());
} catch (Exception e) {
logger.error("failed", e);
}
}
Aggregations