amazonweb服务-如何使用Java删除DynamoDB中的项目



我知道这听起来像是一个简单的问题,但由于某种原因,我无法在网上或通过StackOverflow找到明确的答案。

我有一个名为"ABC"的DynamoDB表。主键是字符串形式的"ID",其他属性之一是字符串形式"Name"。如何使用Java从该表中删除项目?

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

我有一个验证异常:

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)

如果我需要知道哈希密钥才能删除DynamoDB表中的项目,我想我可能需要重新设计我的数据库才能有效地删除项目。

我的桌子是这样的:如果是这样的话,啊。。。我想我需要重新设计我的数据库表。

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

基本上,我想轻松删除日期为"2015-06-21"的所有交易。如何在不必处理哈希密钥ID的情况下简单快速地完成此操作

AWS DynamoDB知道作为表哈希键的列。您只需要指定要删除的值。DeleteItemRequest有一个流利的API:

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

对于Kotlin:

我有一张桌子:分区键:account_id(字符串)排序键:message_id(字符串)

要从DynamoDb中删除项目,我执行以下操作:

    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)
    }

相关内容

  • 没有找到相关文章

最新更新