CosmosPatchOperations (Azure SDK for Java Reference Documentation)
CosmosBulkOperations.getPatchItemOperation(String, PartitionKey, CosmosPatchOperations)
which requires the id of the item to be patched, cosmos patch instance, partition key and CosmosBulkItemRequestOptions
(if-any) and follow remaining steps to execute bulk operations. Let's assume this is the JSON for which we want to run patch operation. { a : "xyz" b : { c : "efg: d : 4 e : [0, 1, 2 , 3] } }
Method Summary
All MethodsStatic MethodsInstance MethodsConcrete Methods
This performs one of the following functions, depending upon what the target location references: 1.
Initializes a new instance ofCosmosPatchOperations
that will contain operations to be performed on a item atomically.This increment the value at the target location.
This increment the value at the target location.
This moves the value of an object from the source to a destination.
This removes the value at the target location.
This replaces the value at the target location with a new value.
This sets the value at the target location with a new value.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Method Details
create
Initializes a new instance ofCosmosPatchOperations
that will contain operations to be performed on a item atomically. Returns: A new instance ofCosmosPatchOperations
.add
This performs one of the following functions, depending upon what the target location references: 1. Target location specifies an array index, a new value is inserted into the array at the specified index. 2. Target location specifies an object member that does not already exist, a new member is added to the object. 3. Target location specifies an object member that does exist, that member's value is replaced. For the above JSON, we can have something like this:
Type Parameters:CosmosPatchOperations cosmosPatch = CosmosPatchOperations.create(); cosmosPatch.add("/b/e", 15); // will add a value to the array, so /b/e array will become [0, 1, 2, 3, 15] cosmosPatch.add("/a", "new value"); // will replace the value cosmosPatch.add("/b/e/1", 10); // will change value of the /b/e array to [0, 10, 2, 3]
This operation is not idempotent for scenario 1 and 2. For 3rd it is as the final value will be the value provided here.T
- The type of item to be added. Parameters:path
- the operation path.value
- the value which will be added. Returns: same instance ofCosmosPatchOperations
remove
This removes the value at the target location. For the above JSON, we can have something like this:
Parameters:CosmosPatchOperations cosmosPatch = CosmosPatchOperations.create(); cosmosPatch.remove("/a"); cosmosPatch.remove("/b/e/3"); // will remove 4th element of /b/e array
This operation is not idempotent. Since once applied, next time it will return bad request due to path not found.path
- the operation path. Returns: same instance ofCosmosPatchOperations
replace
This replaces the value at the target location with a new value. For the above JSON, we can have something like this:
Type Parameters:CosmosPatchOperations cosmosPatch = CosmosPatchOperations.create(); cosmosPatch.replace("/a", "new value"); // will replace "xyz" to "new value" cosmosPatch.replace("/b/e/1", 2); // will replace 2nd element of /b/e array to 2
This operation is idempotent as multiple call execution replace to the same value.T
- The type of item to be replaced. Parameters:path
- the operation path.value
- the value which will be replaced. Returns: same instance ofCosmosPatchOperations
move
This moves the value of an object from the source to a destination. This performs the following based on different cases: 1. Source location points to an object as value, moves the entire object to the target location. 2. Target location specifies an object member that does not already exist, a new member is added to the object. Its value is set to the value of the source location. For the above JSON, we can have something like this:
Parameters:CosmosPatchOperations cosmosPatch = CosmosPatchOperations.create(); cosmosPatch.move("/a", "/c/d"); // will move the value of "/a" to "/c/d" cosmosPatch.move("/b/e/1", "/d"); // will move the object at the 2nd element of the array and set it as the value of "/d". cosmosPatch.move("/b", "/c"); //It can also be used as a rename operation since now all values of "/b" will set as values of "/c".
fromPath
- the source path for the operation.toPath
- the destination path for the operation. Returns: same instance ofCosmosPatchOperations
set
This sets the value at the target location with a new value. For the above JSON, we can have something like this:
Type Parameters:CosmosPatchOperations cosmosPatch = CosmosPatchOperations.create(); cosmosPatch.set("/f", "new value"); // will add a new path "/f" and set it's value as "new value". cosmosPatch.set("/b/e", "bar"); // will set "/b/e" path to be "bar".
This operation is idempotent as multiple execution will set the same value. If a new path is added, next time same value will be set.T
- The type of item to be set. Parameters:path
- the operation path.value
- the value which will be set. Returns: same instance ofCosmosPatchOperations
increment
This increment the value at the target location. It's a CRDT operator and won't cause any conflict. For the above JSON, we can have something like this:
Parameters:CosmosPatchOperations cosmosPatch = CosmosPatchOperations.create(); cosmosPatch.increment("/b/d", 1); // will add 1 to "/b/d" resulting in 5.
This is not idempotent as multiple execution will increase the value by the given increment. For multi-region we do support concurrent increment on different regions and the final value is a merged value combining all increments value. However if multiple increments are on the same region, it can lead to concurrency issue which can be retried.path
- the operation path.value
- the value which will be incremented. Returns: same instance ofCosmosPatchOperations
increment
This increment the value at the target location. For the above JSON, we can have something like this:
Parameters:CosmosPatchOperations cosmosPatch = CosmosPatchOperations.create(); cosmosPatch.increment("/b/d", 3.5); // will add 3.5 to "/b/d" resulting in 7.5.
This is not idempotent as multiple execution will increase the value by the given increment. For multi-region we do support concurrent increment on different regions and the final value is a merged value combining all increments values. However if multiple increments are on the same region, it can lead to concurrency issue which can be retried.path
- the operation path.value
- the value which will be incremented. Returns: same instance ofCosmosPatchOperations
Visit the Azure for Java Developers site for more Java documentation, including quick starts, tutorials, and code samples.
ncG1vNJzZmiZqqq%2Fpr%2FDpJuom6Njr627wWeaqKqVY8SqusOorqxmnprBcHDWnploopGrrnCt2a6pnmWTpMCuu9Joo5qslajBcK%2FOpmaasqWnsnCvzqykqKtforylscusZnyno6K8tJzArZqhh6Cav6LAyKilrGaYqbqt