8

I want to update specific Item(Only One data from row),

How can I update item in DynamoDB.?

login_id is my primary key. I pass login_id and other boolean value. I want to set boolean value true according to that login-id.

How can I do that?

I tried this code.

LinkedHashMap inputHashMap = (LinkedHashMap) input;

        login_id = (String) inputHashMap.get("login_id");
        isUserVerified = (Boolean) inputHashMap.get("isUserVerified");

        DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());

        Table tableUserDetails = dynamoDB.getTable(USER_DETAILS_TABLE);
        Table tableOtpStatus = dynamoDB.getTable(OTP_DETAILS_TABLE);

        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("login_id", login_id);

        try{

            UpdateItemOutcome outcome = tableUserDetails.updateItem(updateItemSpec);
            System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
        }catch(Exception e){
            System.err.println(e.getMessage());
        }

While executing above code I got below exception.

The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException;

1 Answer 1

8

Looks like you're missing the update expression. Assuming isUserVerified is part of the primary key, it should be something like:

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
      .withPrimaryKey("login_id", login_id)
      .withUpdateExpression("set isUserVerified = :val")
      .withValueMap(new ValueMap()
         .withBoolean(":val", true));

If isUserVerified not part of the key then just remove it from the .withPrimaryKey() statement.

You can find more here.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer. I already solved problem same as this way. Thanks for information.
Hey can you give me example, If I want to update more than one value based on login_id (Primary Key).
@ChiragSavsani may I know from which library I can get UpdateItemSpec?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.