java - 通过 dynamoDb 表的 Id(hashKey) 检索数据时出错

标签 java amazon-web-services amazon-dynamodb

我正在尝试根据 Id(HashKey) 从 DynamoDB 表中获取数据。

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

public Association fetchById(String id) {
        DynamoDBConfig dynamoDBConfig = new DynamoDBConfig();
        DynamoDBMapper mapper = new DynamoDBMapper(getDBClient());
        Association association = new Association();
        association.setId(id);
        Association scanResult = null;
        try {
            scanResult = mapper.load(association);
        }
        catch (Exception exception){
            throw exception;
        }
        return scanResult;
    }

调用 fetchById("123")。其中“123”是表的现有 ID 它向我抛出以下错误:-

{
  "errorMessage": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: IT6U9BJ0RAJUDPMLGGR67C542VVV4KQNSO5AEMVJF66Q9ASUAAJG)",
  "errorType": "com.amazonaws.AmazonServiceException",
  "stackTrace": [
    "com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1305)",
    "com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:852)",

DYNAMODB 表详细信息:-

enter image description here

enter image description here

我已在 AWS 文档中搜索了此问题,但找不到任何解决此问题的解决方案。你们中有人遇到过此问题吗?

最佳答案

如果您意识到,您已将 Name 定义为分区键。但是在代码中,您尝试通过属性 Id 加载项目。

这是不可能的,因为 DynamoDB 只允许您通过主键加载项目,在您的情况下,主键只是分区键(属性 Name)。

因此,您必须修改表以将 Id 设置为分区键,或者创建辅助全局索引来完成此操作: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

关于java - 通过 dynamoDb 表的 Id(hashKey) 检索数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899754/

相关文章:

java - 无法识别Java 1.7的夏令时更改

java - 如何启用触摸按钮时的声音效果?

java - 模式匹配器与字符串拆分器,我应该使用哪个?

amazon-web-services - 如何使用 AWS CDK 将元数据添加到 IAM 策略?

amazon-web-services - 以编程方式取消对 aws s3 中文件的加密

javascript - 如何将在 javascript 中创建的数组传递给另一个 jsp,然后在该 jsp 的 java 函数中使用该数组?

amazon-web-services - AWS SAM : Nested Stacks, 从根堆栈引用 API 网关

amazon-dynamodb - dynamodb 扫描 : filter all records where attribute does not exist

amazon-web-services - 在策略中列出 dynamo 的所有权限将不起作用,但通配符可以 dynamodb :*

javascript - AWS Dynamodb不通过 Node js同步获取数据