Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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

How can I update an 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 the 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

0 votes
by (44.4k points)

If isUserVerified part of the primary key, make it something like this:

UpdateItemSpec updateItemSpec = new UpdateItemSpec()

      .withPrimaryKey("login_id", login_id)

      .withUpdateExpression("set isUserVerified = :val")

      .withValueMap(new ValueMap()

         .withBoolean(":val", true));

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

You can find more in this documentation.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories

...