Write Records
PutResult put(PutRequest request)
Function
Adds or modifies a record of the table, which can include all or part of the attributes, or specify the conditions, and writing becomes successful only when the conditions are met
Consumes one unit of write quota, and if a condition is met, one unit of read quota is also consumed (in addition, if an EAGER-type secondary index is created, each Eager secondary index needs 1 additional read quota)
Method parameters
request : PutRequest : required
PutRequest includes the following sections 1.tableName : String : required
Specifies which table to write
2.record : Map< String, Datum > : required
When mapping the attribute name to the attribute value, record must contain the entity group key (if any) and the attribute of the primary key, that is, it must contain the data key of any row and write the row data into the record value
3.condition : SimpleCondition : optional
Specifies the condition of successful writing, ie it can be successfully written when the condition is met, which is the check and set semantics of the atom
The condition may include: specifying that an attribute of the row is greater than, greater than or equal to, equal to, less than or equal to, less than a certain value, or whether the line already exists
Method return value
putResult : PutResult
putResult is only success's one boolean value that indicates if writing is successful or not
If the invocation times out, it cannot be determined whether the record has been written or not
Exception error code
INTERNAL_ERROR(1): Server exception
ACCESS_DENIED(4): User does not have write permission for this table
VALIDATION_FAILED(5): record does not completely contain the entity group key (if any) and the primary key attributes, or the specified row does not exist, or the row specified in the condition does not exist
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
record must contain the entity group key (if any) and the attributes of the primary key
In particular, for a lazy index, all written attribute sets must contain all or zero index attributes, ie it is not allowed to write only some of their attributes.
Example
With the Example table, the sample code for writing record is provided below
$categories = array("work", "travel", "food");
$tableName = 'php-note';
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)]),
)));
// write
$tableClient->put($insert);
$put = $insert;
$put->record["version"] = DatumUtil::datum($version + 1);
$put->record["content"] = DatumUtil::datum("new content $i");
$put->record["mtime"] = DatumUtil::datum((new \DateTime())->getTimestamp());
$put->condition = new SimpleCondition(array(
"operator" => OperatorType::EQUAL,
"field" => "version",
"value" => DatumUtil::datum($version)
));
// wirte with condition, write will succeed if no other write for same record
$tableClient->put($put);
echo "update note without conflict? " . $result->success . "\n";
}