Batch operations
BatchResult batch(BatchRequest request)
Function
Batch read/write operations consume corresponding read/write quotas. Modifying the same row of data in multiple operations in the same batch may result in undefined behavior (data inconsistency),
It should be avoided, otherwise if a batch contains the same row of read/write operations, the order of execution is uncertain and not recommended for use
Also, note the following two points:
Sub-operation in the same batch do not guarantee order
The batch operation does not guarantee atomicity. When returning successful, all sub-operations are successful. If the return is an exception, it is possible to have partial success and partial failure
Method parameters
request : BatchRequest : required
BatchRequest includes a list of available PutRequest, RemoveRequest, GetRequest and IncrementRequest
1.items : List< BatchRequestItem > : required
BatchRequestItem includes BatchOp and Request
The enumeration type BatchOp (PUT, GET, REMOVE, INCREMENT) specifies the operation type of the request,
Correspondingly, use putRequest, getRequest, removeRequest and incrementRequest to construct the Request parameter
Method return value
batchResult : BatchResult
BatchResult includes a list with BatchResultItems that represents the execution result of each request of the batch
items : List< BatchResultItem >
Returns the result of each sub-operation
Exception error code
INTERNAL_ERROR(1): Server exception
ACCESS_DENIED(4): User does not have read permission for this table
VALIDATION_FAILED(5): The request parameter setting in batchRequest is incorrect
THROUGHPUT_EXCEED(8): The current read/write speed has exceeded the read/write quota for this table
RESOURCE_NOT_FOUND(9): Specified table does not exist
Limitation
Each sub-operation must be the same table
put, remove request does not support conditionals
The range of sub-operands for a batch operation is [ 1, 100 ]
Example
Example table. Below is an example code for bulk operation.
$categories = array("work", "travel", "food");
$tableName = 'php-note';
$batch = array();
for ($i = 0; $i < 20; $i++) {
$version = 0; // initial version
$insert = new PutRequest(array(
"tableName" => $tableName,
"record" => Array(
"userId" => DatumUtil::datum("user1"),
"noteId" => DatumUtil::datum($i),
"title" => DatumUtil::datum("Title $i"),
"content" => DatumUtil::datum("note $i"),
"version" => DatumUtil::datum($version),
"mtime" => DatumUtil::datum((new \DateTime())->getTimestamp()),
"category" => DatumUtil::datum($categories[rand(0, sizeof($categories) - 1)]),
)));
$batchItem = new BatchRequestItem(array(
"action" => BatchOp::PUT,
"request" => new Request(array(
"putRequest" => $insert
))
));
$batch[$i] = $batchItem;
}
$batchRequest = new BatchRequest(array(
"items" => $batch
));
$tableClient->batch($batchRequest);
Text
XPath: /pre/code