Java:无法删除dynamodb项目值



我正在尝试删除一个名为"dog"从表动物上的所有项中取出,或者使用以下代码:

Table table= dynamoDB.getTable("animals");
ScanRequest scanRequest = new ScanRequest().withTableName("animals");
ScanResult result = client.scan(scanRequest);

for (Map<String, AttributeValue> item : result.getItems()){
table.updateItem(new PrimaryKey("<Primary partition key name>", item.get("<Primary partition key name>"),"<Primary sort key name>", item.get("<Primary sort key name>")), 
new AttributeUpdate("dog").delete());
}

但是我得到了:

Exception in thread "main" java.lang.RuntimeException: value type: class com.amazonaws.services.dynamodbv2.model.AttributeValue
.
.
.
Caused by: java.lang.UnsupportedOperationException: value type: class com.amazonaws.services.dynamodbv2.model.AttributeValue

我在这里做错了什么?

顺便说一下,我也试过:

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("<Primary partition key name>", item.get("<Primary partition key name>"),"<Primary sort key name>", item.get("<Primary sort key name>"))
.withUpdateExpression("REMOVE dog");
table.updateItem(updateItemSpec);

却得到了相同的异常。(也尝试了DELETE而不是REMOVE)

您正在使用旧的V1 DynamoDB API,这不是最佳实践。最好使用Amazon DynamoDB V2 Java API。

Java 2的AWS SDK。X是对版本1的主要重写。X代码库。它建立在Java 8+之上,并增加了几个经常被要求的特性。其中包括对非阻塞I/O的支持以及在运行时插入不同HTTP实现的能力。

如果您不熟悉Java V2的AWS SDK,请参见:

开始使用AWS SDK for Java 2.x

对于这个用例(正在修改一个项),解决方案是使用Enhanced Client,它允许您将Java对象映射到表。有关更多信息,请参见:

映射DynamoDB表中的项

使用增强型客户端,您可以使用如下代码修改项:

package com.example.dynamodb;

import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import java.time.Instant;

/*
* Prior to running this code example, create an Amazon DynamoDB table named Customer with these columns:
*   - id - the id of the record that is the key
*   - custName - the customer name
*   - email - the email value
*   - registrationDate - an instant value when the item was added to the table
*  Also, ensure that you have setup your development environment, including your credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class EnhancedModifyItem {
public static void main(String[] args) {

String usage = "Usage:n" +
"    UpdateItem <key> <email> nn" +
"Where:n" +
"    key - the name of the key in the table (id120).n" +
"    email - the value of the modified email column.n" ;
if (args.length != 2) {
System.out.println(usage);
System.exit(1);
}
String key = args[0];
String email = args[1];
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder()
.region(region)
.build();
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
.dynamoDbClient(ddb)
.build();
String updatedValue = modifyItem(enhancedClient,key,email);
System.out.println("The updated name value is "+updatedValue);
ddb.close();
}

public static String modifyItem(DynamoDbEnhancedClient enhancedClient, String keyVal, String email) {
try {
//Create a DynamoDbTable object
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
//Create a KEY object
Key key = Key.builder()
.partitionValue(keyVal)
.build();
// Get the item by using the key and update the email value.
Customer customerRec = mappedTable.getItem(r->r.key(key));
customerRec.setEmail(email);
mappedTable.updateItem(customerRec);
return customerRec.getEmail();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
}

您可以在这里找到这个V2示例和其他用于Amazon DYnamoDB的示例:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/dynamodb

我找到了这个问题的解决方案,我不敢相信我错过了....记住,孩子们,在调试时必须查看实际值!

而不是直接使用:

item.get("<property name>")

你需要显式地请求一个String,像这样:

item.get("<property name>").getS()

最新更新