Search in sources :

Example 1 with CosmosPatchItemRequestOptions

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);
    }
}
Also used : CosmosPatchItemRequestOptions(com.azure.cosmos.models.CosmosPatchItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) JsonNode(com.fasterxml.jackson.databind.JsonNode) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with CosmosPatchItemRequestOptions

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);
    }
}
Also used : CosmosPatchItemRequestOptions(com.azure.cosmos.models.CosmosPatchItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations)

Example 3 with CosmosPatchItemRequestOptions

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);
    }
}
Also used : CosmosPatchItemRequestOptions(com.azure.cosmos.models.CosmosPatchItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations) Child(com.azure.cosmos.examples.common.Child)

Example 4 with CosmosPatchItemRequestOptions

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);
    }
}
Also used : CosmosPatchItemRequestOptions(com.azure.cosmos.models.CosmosPatchItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations)

Example 5 with CosmosPatchItemRequestOptions

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);
    }
}
Also used : CosmosPatchItemRequestOptions(com.azure.cosmos.models.CosmosPatchItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations)

Aggregations

Family (com.azure.cosmos.examples.common.Family)9 CosmosPatchItemRequestOptions (com.azure.cosmos.models.CosmosPatchItemRequestOptions)9 CosmosPatchOperations (com.azure.cosmos.models.CosmosPatchOperations)9 PartitionKey (com.azure.cosmos.models.PartitionKey)9 Child (com.azure.cosmos.examples.common.Child)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1