java:无法删除dynamodb项值

3phpmpom  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(500)

我试图从表动物上的所有项目中删除一个名为“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);

但也有同样的例外(还尝试了删除而不是删除)

56lgkhnf

56lgkhnf1#

您使用的是旧的v1 dynamodbapi,这不是最佳实践。使用AmazonDynamodbv2JavaAPI是更好的实践。
AWSSDKforJava2.x是对Version1.x代码库的主要重写。它构建在Java8+之上,并添加了几个经常请求的特性。其中包括对非阻塞i/o的支持,以及在运行时插入不同http实现的能力。
如果您不熟悉aws sdk for java v2,请参阅:
开始使用aws sdk for java 2.x
对于这个用例(修改一个项),解决方案是使用增强型客户机,它允许您将java对象Map到表。有关详细信息,请参阅:
dynamodb表中的Map项
使用增强型clint,您可以使用以下代码修改项目:

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> \n\n" +
                "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

相关问题