简体   繁体   中英

How to set default value of a new attribute for earlier items in dynamodb table?

I have a table with only hash key, now use case requires to create a GSI with creationDate as range key.

I am achieving this by specifying creationDate value as a number in all new items.

But the table already has about 5000 entries with no value set for creationDate and those entries are not included in the result of scan or query operation on GSI. As a solution to this I want to use a default value for all entries with currently no value for creationDate. How do I do this using dynamodbmapper in Java, or is there a way to do it using AWS console ?

The existing items have to be updated individually.

  • DynamoDB API doesn't allow to update item without the Hash key value either using UpdateItemSpec or DynamoDBMapper
  • DynamoDB doesn't have a feature to default some value when the new attribute is added to the table using UpdateTableSpec class

Update the item if the attribute doesn't exists for the given Hash key:-

If the attribute already exists, the following example does nothing; otherwise it sets the attribute to a default value.

    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("orderId", "75246c41-02c7-48c4-8d4c-d202483e6d2b")
            .withReturnValues(ReturnValue.ALL_NEW).withUpdateExpression("set #createDate = if_not_exists (#createDate, :val1)")
            .withNameMap(new NameMap().with("#createDate", "createDate"))
            .withValueMap(new ValueMap().withNumber(":val1", 1));

    UpdateItemOutcome outcome = table.updateItem(updateItemSpec);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM