How to determine if a DynamoDB item was indeed deleted?

半城伤御伤魂 提交于 2021-02-07 02:00:25

问题


DynamoDB provides an API for deleting items. In the returned DeleteItemOutcome and DeleteItemResult there is no field or method to determine if the key was found and the item was indeed deleted.

The only way to find out if the item was indeed present and deleted, is to request the items' attributes:

new DeleteItemSpec() .withPrimaryKey("key","1") .withReturnValues(ReturnValue.ALL_OLD))

This, however, consumes extra read capacity. Is there a more efficient way to check the delete result - key found and deleted / invalid key?


回答1:


DeleteItemResult#getAttributes() is the way to determine if a DeleteItem operation has actually deleted an item, or not.

If you specify ReturnValue.ALL_OLD and the item was deleted, a map of item attributes is returned, otherwise and empty map is returned. This is the only way to know for sure if the operation was successful. No other confirmation is returned by the API.

Keep in mind that a DeleteItem operation will consume a minimum of 1 write capacity unit every time. If the deleted item is larger than 1KB, consumed capacity will be more than 1.

For reference: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/CapacityUnitCalculations.html#ItemSizeCalculations.Writes




回答2:


Try using conditional expressions like

attribute_exists(my_key)

If element doesn't exist conditional check error will be raised



来源:https://stackoverflow.com/questions/46464303/how-to-determine-if-a-dynamodb-item-was-indeed-deleted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!