Search in sources :

Example 1 with Child

use of com.azure.cosmos.examples.common.Child 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 2 with Child

use of com.azure.cosmos.examples.common.Child 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 3 with Child

use of com.azure.cosmos.examples.common.Child 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)

Aggregations

Child (com.azure.cosmos.examples.common.Child)3 Family (com.azure.cosmos.examples.common.Family)3 CosmosPatchItemRequestOptions (com.azure.cosmos.models.CosmosPatchItemRequestOptions)3 CosmosPatchOperations (com.azure.cosmos.models.CosmosPatchOperations)3 PartitionKey (com.azure.cosmos.models.PartitionKey)3