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());
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations