The number of conditions on the keys is invalid - Error in dynamo item update using DynamoDBMapper

299 Views Asked by At

I am new in dynamo db. I am trying to update the dynamo item amt attribute using the following expression.

TransactionWriteRequest transactionWriteRequest = new TransactionWriteRequest();

HashMap<String, AttributeValue> attributeValues = new HashMap<>();
attributeValues.put(":amount",  new AttributeValue().withN(amount));
// key
Map<String, AttributeValue> key = new HashMap<>();
key.put("pk", new AttributeValue().withS(pk));
key.put("sk", new AttributeValue().withS(sk));

UpdateItemRequest updateItemRequest = new UpdateItemRequest()
    .withKey(key)
    .withTableName("dynamo-test")
    .withUpdateExpression("SET amt = amt + :amount")
    .withExpressionAttributeValues(attributeValues);

transactionWriteRequest.addUpdate(updateItemRequest);

dynamoDBMapper.transactionWrite(transactionWriteRequest);

There are multiple updates which are getting executed in transactions. On executing this, it is throwing the following exception.

AmazonDynamoDBException: The number of conditions on the keys is invalid

I am stuck here, not finding any error in the above code. Please help here.

Thanks in advance

1

There are 1 best solutions below

9
On

I believe you are messing up two clients, you should not use Mapper Client if you are not mapping the data to a class object. Use the DynamoDB low level client which your instantiated to make the transaction request.

Furthermore, ensure that your table dynamo-test had a partition key of pk and sort key of sk both of type String.