I’m trying to implement with DynamoDB a way to allow items to be only inserted and NOT updated/replaced (to achieve some level of transaction control). Therefore, I have the following DB Schema configured in AWS DynamoDB:
PartitionKey="driveUniqueId (String)"
SortKey="createTime (String)"
Now, if I run the following snippet of code, the operation works and the record is created.
String currentTime = LocalTime.now().toString();
dynamoDbClient.transactWriteItems(TransactWriteItemsRequest.builder()
.transactItems(
TransactWriteItem.builder().put(
Put.builder()
.tableName(tableName)
.item(Map.of(
"driveUniqueId", AttributeValue.builder().s("789").build(),
"createTime", AttributeValue.builder().s(currentTime).build()))
.build())
.build())
.build());
However, If I run the following snippet of code adding the conditionCheck
to prevent replaces, then I get the following error:
dynamoDbClient.transactWriteItems(TransactWriteItemsRequest.builder()
.transactItems(
TransactWriteItem.builder().put(
Put.builder()
.tableName(tableName)
.item(Map.of(
"driveUniqueId", AttributeValue.builder().s("123").build(),
"createTime", AttributeValue.builder().s(currentTime).build()))
.build())
.build(),
TransactWriteItem.builder().conditionCheck(
ConditionCheck.builder()
.tableName(tableName)
.key(Map.of(
"driveUniqueId", AttributeValue.builder().s("123").build()))
.conditionExpression("attribute_not_exists(driveUniqueId)")
.build())
.build())
.build());
/*
Error:
CancellationReason(Code=None)
CancellationReason(Code=ValidationError, Message=The provided key element does not match the schema)
*/
I don’t understand why the condition specified can’t understand the "driveUniqueId" as part of the schema in the second write operation.
I’m running this code using Quarkus, Java 17, and AWS SDK 2.17.291.
Any ideas why my conditionCheck
is wrong?
2
Answers
This is my final solution based on @Lee Hanningan response.
Your condition check is causing a validation error because you did not include the items sork key
createTime
.As with any write to DynamoDB you must include the full primary key, partition and sort key.
Furthermore, you possibly do not need to use transactions, single item operations are ACID compliant, so you could simply use a
PutItem
with a condition check.