DynamoDB-如何检索和删除(弹出)项目? [英] DynamoDB - how to retrieve and delete (pop) an item?

查看:228
本文介绍了DynamoDB-如何检索和删除(弹出)项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发用Flask编写的应用程序,该应用程序通过boto访问由Amazon的DynamoDB支持。

I am working on an application written in Flask and backed by Amazon's DynamoDB accessed through boto.

对于特定的用例,我们需要从a中检索一个值表,然后使其对其他用户不可用。

For a specific use case, we need to retrieve a value from a table and then make it unavailable for other users.

但是,通过检索然后删除该值,在检索和删除之间可能会出现竞争条件。

However, by retrieving and then deleting the value, a race-condition could occur in between the retrieval and deletion.

有什么方法可以从表中检索项目并立即以原子方式删除或更新它?

Is there any way to retrieve an item from a table and immediately delete or update it in an atomic fashion?

推荐答案

如果您的逻辑是


  1. 获取项目

  2. delete

没有任何其他逻辑来确定是否应该删除,那么您实际上可以立即发送删除请求,下面是示例(尚未检查,通常取自: http://docs.aws .amazon.com / amazondynamodb / latest / developerguide / LowLevelJavaItemCRUD.html

without any additional logic to determine whether deletion should occur, then you can actually send delete request immediately, here is example (I haven't checked it, mostly take from: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelJavaItemCRUD.html)

HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("Id", new AttributeValue().withN("101"));

DeleteItemRequest deleteItemRequest = new DeleteItemRequest()
    .withTableName(tableName)
    .withKey(key)
    .withReturnValues(ReturnValue.ALL_OLD);

DeleteItemResult deleteItemResult = client.deleteItem(deleteItemRequest);
Map<String,AttributeValue> deletedItem = deleteItemResult.getAttributes();

文档:

withReturnValues

getAttributes

这篇关于DynamoDB-如何检索和删除(弹出)项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆