CustardBun
CustardBun

Reputation: 3867

DynamoDB Java AWS SDK - UpdateItem simplification possible?

I am new to using AWS and am writing something that will add an attribute to the provided item given some keyAttributes. I got it working, but the code I ended up writing just looks unintuitive and silly to me. (I just based it off of documentation I've been finding online.)

I was able to do a successful basic update of an item in a dynamoDB table with this code:

final AttributeValue fulfilled = new AttributeValue().withBOOL(true);

final UpdateItemRequest updateItemRequest = new UpdateItemRequest()
     .withTableName(tableName)
     .withKey(keyAttributes)
     .withUpdateExpression("SET fulfilled = :fulfilled")
     .withExpressionAttributeValues(ImmutableMap.of(":fulfilled", fulfilled));

final UpdateItemResult result = dynamoClient.updateItem(updateItemRequest);

Is there anything I can do to cut it down to just the barebones thing I'm trying to do here: just adding a new attribute "fulfilled" to an item, set to true?

Upvotes: 0

Views: 1454

Answers (1)

Matthew Pope
Matthew Pope

Reputation: 7679

An UpdateItemRequest must always contain the table name and the key attributes. (Otherwise how would DynamoDB know which item to update?)

You can simplify it, though, by getting rid of the ExpressionAttributeValues, like this:

final UpdateItemRequest updateItemRequest = new UpdateItemRequest()
        .withTableName(tableName)
        .withKey(keyAttributes)
        .withUpdateExpression("SET fulfilled = TRUE");

final UpdateItemResult result = dynamoClient.updateItem(updateItemRequest);

Upvotes: 1

Related Questions