0

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

Shailendra
  • 528
  • 2
  • 10
  • 21
  • Could you share the schema of the table please. I'm assuming that (as Lee points out) that pk and sk are defined as keys on the table. You cannot perform an Update without specifying the full key -- it is not sufficient that they exist in the table. – amrith Dec 30 '22 at 12:46

1 Answers1

1

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.

Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31
  • pk and sk both are available in my table. can't i write updateExpression using mapper client? – Shailendra Dec 29 '22 at 19:17
  • You can, but you are using low level syntax whereas mapper uses objects. Decide which you want to use, you cannot use a combination. – Leeroy Hannigan Dec 29 '22 at 19:20
  • I couldn't find any reference for using updateExpression using DynamoDBMapper, if you can help. I would be very thankful. I want to use DynamoDBMapper only. – Shailendra Dec 29 '22 at 19:22
  • Why do you want to use Mapper if you are not mapping the data to an object? – Leeroy Hannigan Dec 29 '22 at 19:24
  • In transactionWriteRequest there are other put operation objects which are mapped to a class, and one Update operation as written above. i wan't these put and update execution in a transaction. If I had only update, I could have used low level client. – Shailendra Dec 29 '22 at 19:29
  • Then use the proper syntax for the UpdateItem where you model it to a class object, just as you do with the PutItems. – Leeroy Hannigan Dec 29 '22 at 19:31
  • main problem is doing this withUpdateExpression("SET amt = amt + :amount") in modeled object. i can handled this in application (which i don't want), update the field amount and save the object. – Shailendra Dec 29 '22 at 19:50
  • Like I said, if you need to use Mapper then use an object of the the class you are mapping to. You're defining keys as you would in the low level client. – Leeroy Hannigan Dec 29 '22 at 20:10
  • https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.Transactions.html – Leeroy Hannigan Dec 29 '22 at 20:10