从dynamoDB表删除数据时出错 [英] Error while deleting data from dynamoDB table

查看:135
本文介绍了从dynamoDB表删除数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图基于Id(HashKey)从DynamoDB表中删除数据。

I am trying to delete the data from my DynamoDB table based on the Id(HashKey).

Association.java

@DynamoDBTable(tableName = "Association")
public class Association {

    private String id;
    private String name;
    private String adminName;
    private String email;
    private String url;
    private String contactNumber;
    private String password;

    public Association() { }

    public Association(String name, String adminName, String email, String url,
                       String contactNumber, String password) {
        this.name = name;
        this.adminName = adminName;
        this.email = email;
        this.url = url;
        this.contactNumber = contactNumber;
        this.password = password;
    }

    public Association(String id, String name, String adminName, String email, String url,
                       String contactNumber, String password) {
        this.id = id;
        this.name = name;
        this.adminName = adminName;
        this.email = email;
        this.url = url;
        this.contactNumber = contactNumber;
        this.password = password;
    }

    @DynamoDBHashKey(attributeName = "Id")
    @DynamoDBAutoGeneratedKey
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @DynamoDBAttribute(attributeName="Name")
    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    @DynamoDBAttribute(attributeName="AdminName")
    public String getAdminName() { return adminName; }

    public void setAdminName(String adminName) { this.adminName = adminName; }

    @DynamoDBAttribute(attributeName="Email")
    public String getEmail() { return email; }

    public void setEmail(String email) { this.email = email; }

    @DynamoDBAttribute(attributeName="Url")
    public String getUrl() { return url; }

    public void setUrl(String url) { this.url = url; }

    @DynamoDBAttribute(attributeName="ContactNumber")
    public String getContactNumber() { return contactNumber; }

    public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; }

    @DynamoDBAttribute(attributeName="Password")
    public String getPassword() { return password; }

    public void setPassword(String password) { this.password = password; }

}

AssociationRepository.java:-

private AmazonDynamoDBClient getDynamoDBClient(){
        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setRegion(Region.getRegion(REGION));
        client.setEndpoint(EndPoint);
        return client;
    }

private void deleteAssociation(String id) {
        try {
            DynamoDBConfig dynamoDBConfig = new DynamoDBConfig();
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBConfig.getDBClient());
            Association associationToDelete = new Association();
            associationToDelete.setId(id);
            // Delete the item.
            mapper.delete(associationToDelete);
        } catch (Exception exception) {
            System.out.println(exception);
            throw exception;
        }
    }

我遇到以下错误:-

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: No method annotated with interface com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey for class class Association

我已经在AWS文档中搜索了此文件,但找不到解决此问题的任何解决方案。

I have searched the AWS docs for this but could not find any solution that fixes this.Does anyone of you ever faced this issue?

推荐答案

问题与我在另一篇文章中提到的相同,在DynamoDB中,您不能使用属性来处理项目不是主要的y(仅在您的方案中使用分区键)。

The issue is the same I mentioned on your other post, in DynamoDB you can’t work an item using attributes which are not the primary key (Partition Key only in your scenario).

此外,要删除某项,必须通过其主键来执行,因此,在您的情况下为属性Name。

Moreover, to delete an item it is mandatory to do it by its primary key, so in your case the attribute Name.

尝试通过设置名称来执行相同的操作,该错误不是很具描述性,因此也许还有另一个潜在的问题。但是,如果您不满足上述要求,它将永远无法使用。

Try to do same operation by setting the name, the error is not very descriptive so maybe there is another underlying issue. However, if you don’t meet above requirement it will never work.

这篇关于从dynamoDB表删除数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆