Python boto DynamoDB:如何防止使用批处理写入覆盖项目?
我正在使用并且正在批量将项目写入表中。但是,我无法阻止DynamoDB覆盖现有项的属性。我宁愿这个过程失败 该表具有以下架构:Python boto DynamoDB:如何防止使用批处理写入覆盖项目?,python,amazon-web-services,boto,Python,Amazon Web Services,Boto,我正在使用并且正在批量将项目写入表中。但是,我无法阻止DynamoDB覆盖现有项的属性。我宁愿这个过程失败 该表具有以下架构: from boto.dynamodb2.table import Table, HashKey, RangeKey conn = get_connection() t = Table.create( 'intervals', schema=[ HashKey('id'), RangeKey
from boto.dynamodb2.table import Table, HashKey, RangeKey
conn = get_connection()
t = Table.create(
'intervals',
schema=[
HashKey('id'),
RangeKey('start')
],
connection=conn
)
假设我插入一项:
item = {
'id': '4920',
'start': '20',
'stop': '40'
}
t.put_item(data=item)
现在,当我插入带有batch\u write
的新项目时,我想确保DynamoDB不会覆盖现有项目。根据,这应该通过BatchTable
类的put\u item
方法中的overwrite
参数来实现(该参数在下面的示例中用作上下文管理器)
然而,事实并非如此。我的示例中的stop
属性获得一个新值90
。因此上一个值(40
)被覆盖
如果使用表自己的put\u item
方法,则overwrite
参数有效。将其设置为True
将替换stop
值,同时将其设置为False
将导致条件检查失败异常
当使用批处理写入时,我如何才能获得该异常?我认为使用DynamoDB没有任何方法可以做到这一点。批处理API不支持它。BatchTable
对象的put\u item
方法接受overwrite
参数,这是一个错误。如果检查代码,可以看到它对该参数没有任何作用。它被忽略,因为它与它没有任何关系。DynamoDB不支持这个。至少还没有。这不是我所希望的答案:)。我在boto存储库中创建了一个。
new_items = [{
'id': '4920',
'start': '20',
'stop': '90'
}]
with t.batch_write() as batch:
for i in new_items:
batch.put_item(data=i, overwrite=False)