Search in sources :

Example 6 with CosmosPatchItemRequestOptions

use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SamplePatchQuickstart method patchAddArray.

// demonstrates multiple patch (add) operations for an array
private void patchAddArray(String id, String partitionKey) {
    logger.info("Executing Patch with 'add' operation on array");
    CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
    Child c1 = new Child();
    c1.setFamilyName("Andersen");
    c1.setFirstName("Selena");
    c1.setGender("f");
    Child c2 = new Child();
    c2.setFamilyName("Andersen");
    c2.setFirstName("John");
    c2.setGender("m");
    // add to an empty array
    cosmosPatchOperations.add("/children", Arrays.asList(c1, c2));
    Child c3 = new Child();
    c3.setFamilyName("Andersen");
    c3.setFirstName("Shaun");
    c3.setGender("m");
    // add to an existing array to specific index. will add element at that index
    // and
    // shift others
    cosmosPatchOperations.add("/children/1", c3);
    Child c4 = new Child();
    c4.setFamilyName("Andersen");
    c4.setFirstName("Mariah");
    c4.setGender("f");
    // add to an existing array, with index equal to array length. element will be
    // added to end of array
    cosmosPatchOperations.add("/children/3", c4);
    Child c5 = new Child();
    c5.setFamilyName("Andersen");
    c5.setFirstName("Brian");
    c5.setGender("m");
    // to append to end of an array, you can use the convenience character "-". you
    // can skip index calculation
    cosmosPatchOperations.add("/children/-", c5);
    // un-commenting below will result in BadRequestException: array contains 4
    // elements now. an attempt to add element at an index that is greater than the
    // length
    // cosmosPatchOperations.add("/children/5", c4);
    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 7 with CosmosPatchItemRequestOptions

use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SamplePatchQuickstart method patchAddMultiple.

// demonstrates multiple patch (add) operations
private void patchAddMultiple(String id, String partitionKey) {
    logger.info("Executing Patch with multiple 'add' operations");
    CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
    cosmosPatchOperations.add("/vaccinated", false).add("/district", "NY23");
    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 8 with CosmosPatchItemRequestOptions

use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SamplePatchQuickstart method patchSet.

// demonstrates set operation. it is same as add except for array.
private void patchSet(String id, String partitionKey) {
    logger.info("Executing Patch with 'set' operations");
    CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
    // does not exist, will be added (same behavior as add)
    cosmosPatchOperations.set("/vaccinated", false);
    // exists, will be replaced (same behavior as add)
    cosmosPatchOperations.set("/district", "WA5");
    Child c1 = new Child();
    c1.setFamilyName("Andersen");
    c1.setFirstName("Selena");
    c1.setGender("f");
    cosmosPatchOperations.set("/children", Arrays.asList(c1));
    Child c3 = new Child();
    c3.setFamilyName("Andersen");
    c3.setFirstName("Shaun");
    c3.setGender("m");
    // add to an existing array, with index. will substitute element at that index
    // (NOT the same behavior as add)
    cosmosPatchOperations.set("/children/0", c3);
    Child c4 = new Child();
    c4.setFamilyName("Andersen");
    c4.setFirstName("Mariah");
    c4.setGender("f");
    // add to an existing array, with index equal to array length. element will be
    // added to end of array
    cosmosPatchOperations.set("/children/1", c4);
    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 9 with CosmosPatchItemRequestOptions

use of com.azure.cosmos.models.CosmosPatchItemRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SamplePatchQuickstart method patchIncrement.

private void patchIncrement(String id, String partitionKey) {
    logger.info("Executing Patch with 'increment' operations");
    CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
    cosmosPatchOperations.add("/int", 42).increment("/int", 1).increment("/int", -1).add("/float", 42).increment("/float", 4.2).increment("/float", -4.2);
    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)

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