1
votes

When trying to do a delete via AWS Java SDK I get the error

The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 52N303HS3D535K28KSN3R3803VVV4KQNSO5AEMVJF66Q9ASUAAJG)

I have a delete item spec defined that looks like this

DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey("pk", messageId)
            .withConditionExpression("#ip > :val")
            .withNameMap(new NameMap()
                    .with("#ip", "timestamp"))
            .withValueMap(new ValueMap()
                    .withNumber(":val", 0))
            .withReturnValues(ReturnValue.NONE);

And my table is created like this

List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
    attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("pk")
            .withAttributeType(ScalarAttributeType.S));
    attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("timestamp")
            .withAttributeType(ScalarAttributeType.N));

    List<KeySchemaElement> keySchema = new ArrayList<>();
    keySchema.add(new KeySchemaElement()
            .withAttributeName("pk")
            .withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement()
            .withAttributeName("timestamp")
            .withKeyType(KeyType.RANGE));

I'm wondering if the sort key for timestamp is causing this issue. Do I need to specify the timestamp other than > 0?

1

1 Answers

3
votes

The issue is that you must specify both the hash and range key when you delete an object. Your hash key is "pk" and your range key is "timestamp" but you are only passing in the hash key into the withPrimaryKey method.

It looks like you are trying to delete multiple items at a time. This is not possible with DynamoDB. You will first need to do a query on the key and you can apply your condition expression to that to only retrieve the keys of the items you want to delete. However, you will then need to call the delete API individually for each record or use the batch API to delete records in batches while still specifying the hash and range key for each individual item.