Search in sources :

Example 6 with CosmosPatchOperations

use of com.azure.cosmos.models.CosmosPatchOperations 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 CosmosPatchOperations

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

the class SamplePatchQuickstart method patchTransactionalBatchAdvanced.

// demonstrates a variety of patch operation in the context of a transactional
// batch - including a conditional predicate
private void patchTransactionalBatchAdvanced() {
    logger.info("Executing Multiple Patch operations on multiple documents as part of a Transactional Batch");
    String lastName = "testLastName";
    // generating two random Family objects. these will be created and then patched
    // (within a transaction)
    Family family1 = Families.getWakefieldFamilyItem();
    Family family2 = Families.getJohnsonFamilyItem();
    // setting a fixed last name so that we can use it as a partition key for the
    // transactional batch
    family1.setLastName(lastName);
    family2.setLastName(lastName);
    // TransactionalBatch batch = TransactionalBatch.createTransactionalBatch(new
    // PartitionKey(lastName));
    CosmosBatch batch = CosmosBatch.createCosmosBatch(new PartitionKey(lastName));
    batch.createItemOperation(family1);
    batch.createItemOperation(family2);
    CosmosPatchOperations commonPatchOperation = CosmosPatchOperations.create();
    commonPatchOperation.replace("/district", "new_replaced_value");
    // replacing 'district' via patch
    batch.patchItemOperation(family1.getId(), commonPatchOperation);
    batch.patchItemOperation(family2.getId(), commonPatchOperation);
    // if registered is false (predicate/condition), vaccinated status has to be
    // false as well (defining this via conditional patch operation)
    CosmosPatchOperations addVaccinatedStatus = CosmosPatchOperations.create().add("/vaccinated", false);
    // TransactionalBatchPatchItemRequestOptions options = new
    // TransactionalBatchPatchItemRequestOptions();
    CosmosBatchPatchItemRequestOptions options = new CosmosBatchPatchItemRequestOptions();
    options.setFilterPredicate("from f where f.registered = false");
    batch.patchItemOperation(family2.getId(), addVaccinatedStatus, options);
    try {
        CosmosBatchResponse response = container.executeCosmosBatch(batch);
        for (CosmosBatchOperationResult batchOpResult : response.getResults()) {
            if (response.isSuccessStatusCode()) {
                logger.info("{} operation for ID {} was successful", batchOpResult.getOperation().getOperationType().name(), batchOpResult.getItem(Family.class).getId());
            } else {
                logger.info("{} operation failed. Status code: {}", batchOpResult.getOperation().getOperationType().name(), batchOpResult.getStatusCode());
            }
        }
    } catch (Exception e) {
        logger.error("failed", e);
    }
}
Also used : CosmosBatch(com.azure.cosmos.models.CosmosBatch) CosmosBatchPatchItemRequestOptions(com.azure.cosmos.models.CosmosBatchPatchItemRequestOptions) CosmosBatchResponse(com.azure.cosmos.models.CosmosBatchResponse) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations) CosmosBatchOperationResult(com.azure.cosmos.models.CosmosBatchOperationResult)

Example 8 with CosmosPatchOperations

use of com.azure.cosmos.models.CosmosPatchOperations 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 9 with CosmosPatchOperations

use of com.azure.cosmos.models.CosmosPatchOperations 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 10 with CosmosPatchOperations

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

the class SamplePatchQuickstart method patchTransactionalBatch.

private void patchTransactionalBatch() {
    logger.info("Executing Patch operation as part of a Transactional Batch");
    Family family = Families.getAndersenFamilyItem();
    // TransactionalBatch batch = TransactionalBatch.createTransactionalBatch(new
    // PartitionKey(family.getLastName()));
    CosmosBatch batch = CosmosBatch.createCosmosBatch(new PartitionKey(family.getLastName()));
    batch.createItemOperation(family);
    CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
    cosmosPatchOperations.add("/vaccinated", false);
    batch.patchItemOperation(family.getId(), cosmosPatchOperations);
    try {
        CosmosBatchResponse response = container.executeCosmosBatch(batch);
        logger.info("Response code for transactional batch operation: ", response.getStatusCode());
        if (response.isSuccessStatusCode()) {
            logger.info("Transactional batch operation executed for ID {}", response.getResults().get(0).getItem(Family.class).getId());
        }
    } catch (Exception e) {
        logger.error("failed", e);
    }
}
Also used : CosmosBatch(com.azure.cosmos.models.CosmosBatch) CosmosBatchResponse(com.azure.cosmos.models.CosmosBatchResponse) 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)11 CosmosPatchOperations (com.azure.cosmos.models.CosmosPatchOperations)11 PartitionKey (com.azure.cosmos.models.PartitionKey)11 CosmosPatchItemRequestOptions (com.azure.cosmos.models.CosmosPatchItemRequestOptions)9 Child (com.azure.cosmos.examples.common.Child)3 CosmosBatch (com.azure.cosmos.models.CosmosBatch)2 CosmosBatchResponse (com.azure.cosmos.models.CosmosBatchResponse)2 CosmosBatchOperationResult (com.azure.cosmos.models.CosmosBatchOperationResult)1 CosmosBatchPatchItemRequestOptions (com.azure.cosmos.models.CosmosBatchPatchItemRequestOptions)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1