Search in sources :

Example 21 with Family

use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SamplePatchQuickstart method createFamilies.

private void createFamilies(List<Family> families) throws Exception {
    double totalRequestCharge = 0;
    for (Family family : families) {
        // <CreateItem>
        // Create item using container that we created using sync client
        // Use lastName as partitionKey for cosmos item
        // Using appropriate partition key improves the performance of database
        // operations
        CosmosItemRequestOptions cosmosItemRequestOptions = new CosmosItemRequestOptions();
        CosmosItemResponse<Family> item = container.createItem(family, new PartitionKey(family.getLastName()), cosmosItemRequestOptions);
        // </CreateItem>
        logger.info("Created item with request charge of {} within duration {}", item.getRequestCharge(), item.getDuration());
        totalRequestCharge += item.getRequestCharge();
    }
    logger.info("Created {} items with total request charge of {}", families.size(), totalRequestCharge);
    Family family_to_upsert = families.get(0);
    logger.info("Upserting the item with id {} after modifying the isRegistered field...", family_to_upsert.getId());
    family_to_upsert.setRegistered(!family_to_upsert.isRegistered());
    CosmosItemResponse<Family> item = container.upsertItem(family_to_upsert);
    // Get upsert request charge and other properties like latency, and diagnostics
    // strings, etc.
    logger.info("Upserted item with request charge of {} within duration {}", item.getRequestCharge(), item.getDuration());
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey)

Example 22 with Family

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

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

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

use of com.azure.cosmos.examples.common.Family 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)58 PartitionKey (com.azure.cosmos.models.PartitionKey)37 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)19 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)12 CosmosDiagnostics (com.azure.cosmos.CosmosDiagnostics)11 CosmosException (com.azure.cosmos.CosmosException)11 CosmosPatchOperations (com.azure.cosmos.models.CosmosPatchOperations)11 ArrayList (java.util.ArrayList)11 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)10 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)10 CosmosPatchItemRequestOptions (com.azure.cosmos.models.CosmosPatchItemRequestOptions)9 Duration (java.time.Duration)7 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)6 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)6 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)6 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)5 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)5