Error while retrieving data by Id(hashKey) of a dynamoDb table

Sunny :

I am trying to fetch 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;
    }

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

Invoking fetchById("123"). Where "123" is an existing Id of the table It throws the below error to me:-

{
  "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 TABLE DETAILS:-

enter image description here

enter image description here

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?

Alberto Martin :

If you realize, you defined Name as your partition key. However on code, you are trying to load an item by the attribute Id.

That's not possible as DynamoDB only allows you to load an item by its primary key, which in your case is only the partition key (the attribute Name).

So you have to modify either the table to set Id as partition key, or create a secondary global index to accomplish that: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360956&siteId=1