1
votes

I've been attempting to delete an item from a table in DynamoDB through java code, but every attempt I've made results in the same error:

com.amazonaws.AmazonServiceException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException;

My current attempt is very simple and looks like this:

final DynamoDB dynamoDB = new DynamoDB(new  AmazonDynamoDBClient(credentials));  

Table table =dynamoDB.getTable(tableName);    

DeleteItemSpec itemSpec = new  DeleteItemSpec().withPrimaryKey("cognitoId", cognitoId);  
table.deleteItem(itemSpec);

tablename is simply the table name, the credentials have been verified to be correct, and cognitoId is the actual ID of the item I'm trying to delete. The table in question has cognitoId as the primary key and I don't understand why the deletion isn't matching the schema. The table also has a sort key, or range key (I'm not sure what it is because the documentation is quite vague). I've been referring to the documentation here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithTables.html#WorkingWithTables.primary.key

2
This delete item using primary key should help you solve. - Anvesh Vejandla
Try using deleteItem(String hashKeyName, Object hashKeyValue) - Chris Franklin

2 Answers

2
votes

Did you have a sort key while creating a table? If so, then you have to specify the sort key too as you have a composite key on the table. Having a sort key means that you could have multiple records with the same primary key, however the sort key must be unique

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithTables.html#WorkingWithTables.primary.key

The sort key may also be referred to as range or range key in the AWS Dynamo DB documentation and the console.

So your delete item would be like

DeleteItemSpec itemSpec = new DeleteItemSpec().withPrimaryKey("cognitoId", "my_id", "sortKeyField", "sort_key_id");
DeleteItemOutcome outcome = table.deleteItem(itemSpec);
0
votes

FYI this also happens if you provide the wrong key name, e.g.:

DeleteItemSpec itemSpec = new DeleteItemSpec().withPrimaryKey("#NOPENOTMYKEYLOL", "my_id", "sortKeyField", "sort_key_id");
DeleteItemOutcome outcome = table.deleteItem(itemSpec);