0
votes

I have a DynamoDb table, I want to delete an item from the table using the primary and sort key,

Following is my code -

public Boolean deleteItem(String primaryKey, String sortKey) {
        DynamoDBUtil dynamoDBUtil = new DynamoDBUtil();
        AmazonDynamoDB dynamoDBClient = dynamoDBUtil.getDynamoDBClient();
        DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

        Table table = dynamoDB.getTable("demo_table");
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
                .withPrimaryKey(new PrimaryKey("primaryKey", primaryKey))
                .withConditionExpression("primaryKey = :p_key and sortKey = :s_key")
                .withValueMap(new ValueMap()
                .withString(":p_key", primaryKey)
                .withString(":s_key", sortKey));

        table.deleteItem(deleteItemSpec);
        return true;
    }

I get the following error response -

Received error response: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: FHR7236SPHAJEANHBDKTO52Q2FVV4KQNSO5AEMVJF66Q9ASUAAJG)
Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: FHR7236SPHAJEANHBDKTO52Q2FVV4KQNSO5AEMVJF66Q9ASUAAJG)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1588)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1258)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:742)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:716)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2089)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2065)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeDeleteItem(AmazonDynamoDBClient.java:744)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.deleteItem(AmazonDynamoDBClient.java:720)
    at com.amazonaws.services.dynamodbv2.document.internal.DeleteItemImpl.doDeleteItem(DeleteItemImpl.java:96)
    at com.amazonaws.services.dynamodbv2.document.internal.DeleteItemImpl.deleteItem(DeleteItemImpl.java:75)
    at com.amazonaws.services.dynamodbv2.document.Table.deleteItem(Table.java:339)

I double tripple checked, my schema is correct.

1
You can try wrapping the two string parameters in an AttributeValue each. - Barend

1 Answers

7
votes

I think you're confusing the partition (hash) key with the primary key. Your sort (range) key is part of the primary key. So, you'll need to provide it as part of the primary key. Hence, there will be no need for the condition expression.

Primary key = Partition key + sort key

For example:

public Boolean deleteItem(String partitionKey, String sortKey) {
    DynamoDBUtil dynamoDBUtil = new DynamoDBUtil();
    AmazonDynamoDB dynamoDBClient = dynamoDBUtil.getDynamoDBClient();
    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

    Table table = dynamoDB.getTable("demo_table");
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
        .withPrimaryKey("partitionKey", partitionKey, "sortKey", sortKey);

    table.deleteItem(deleteItemSpec);
    return true;
}