简体   繁体   中英

How to delete an item in DynamoDB using Java?

I know this sounds like a simple question, but for some reason I can't find a clear answer online or through StackOverflow.

I have a DynamoDB with a Table named "ABC". The primary key is "ID" as a String and one of the other attributes is "Name" as a String. How can I delete an item from this table using Java?

    AmazonDynamoDBClient dynamoDB;
    .
    .
    .
    DeleteItemRequest dir = new DeleteItemRequest();
    dir.withConditionExpression("ID = 214141").withTableName("ABC");
    DeleteItemResult deleteResult = dynamoDB.deleteItem(dir);

I have a validation exception:

Exception in thread "main" com.amazonaws.AmazonServiceException: 1 validation error detected: Value null at 'key' failed to satisfy constraint: Member must not be null (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: RQ70OIGOQAJ9MRGSUA0UIJLRUNVV4KQNSO5AEMVJF66Q9ASUAAJG)
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1160)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:748)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:467)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:302)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3240)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.deleteItem(AmazonDynamoDBClient.java:972)
    at DynamoDBUploader.deleteItems(DynamoDBUploader.java:168)
    at Main.main(Main.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

If I need to know the Hash Key in order to delete an item in a DynamoDB Table, I think I may need to redesign my database in order to delete items efficiently.

My table looks like this: If that is the case, ahh... I think I need to re-design my database table.

ID | Name | Date | Value
-----------------------------------
1  | TransactionA | 2015-06-21 | 30
2  | TransactionB | 2015-06-21 | 40
3  | TransactionC | 2015-06-21 | 50

Basically, I would like to easily delete all transactions with Date "2015-06-21". How can I do this simply and quickly without having to deal with the Hash Key ID?

AWS DynamoDB knows the column that is hash key of your table. You just need to specify the value to be deleted. DeleteItemRequest has a fluent API for that :

Key keyToDelete = new Key().withHashKeyElement(new AttributeValue("214141"));
DeleteItemRequest dir = new DeleteItemRequest()
    .withTableName("ABC")
    .withKey(keyToDelete);

For Kotlin:

I have table with: Partition key: account_id (String) Sort key: message_id (String)

To delete an item fron DynamoDb I do the following:

    fun deleteMessageById(messageId: String, accountId: String){
        val item = HashMap<String, AttributeValue>()
        item["account_id"] = AttributeValue(accountId)
        item["message_id"] = AttributeValue(messageId)
        val deleteRequest = DeleteItemRequest().withTableName(tableName).withKey(item)
        dynamoConfiguration.amazonDynamoDB().deleteItem(deleteRequest)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM