Boto DynamoDB2条件put_项

2024-07-08 10:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在实际添加新项之前,让put-item检查是否有具有相同HashKey的项。在

根据boto DynamoDB2文档,使用“条件Put”是可能的。在

我试着听从命令,但运气不佳。在

connection.put_item('table',item={'locationId':'a1', 'timestamp': time.time()}, expected={'locationID':False})

错误消息如下。在

^{pr2}$

有没有人对DynamoDBv2有条件的put?在

感谢大家的提前。在


Tags: 文档timeputa1tableconnectionitem条件
2条回答

你需要这样写才能让语法工作。非常糟糕的是,这在任何地方都没有记录。我只是遇到了完全相同的问题,在翻阅javasdk文档时,我不得不尝试50种不同的方法。请注意,如果要使用'Exists': True而不是False,则需要给出一个值。在

connection.put_item(
    'table',
    item={
        'locationId':{'S': 'a1'},
        'timestamp': {'N': str(time.time())
    },
    expected={
        'locationID': {'Exists': False}
    }
    #expected={
    #    'locationID': {'Exists': True, 'Value': {'N': '0'}}
    #}
)

希望有帮助!在

编辑:the question that helped me with the syntax enough to make it workcode in the boto integration tests

注意,put_item的参数expected现在是deprecated

Parameters: expected (map)

There is a newer parameter available. Use ConditionExpression instead.

使用condition_expression代替:

connection.put_item(
    'table',
    item={
        'locationId': {'S': 'a1'}, 
        'timestamp': {'S': str(time.time())}
    },
    condition_expression='attribute_exists(locationId)'
)

也可以链接表达式和使用命名参数,例如:

^{pr2}$

See Performing Conditional Writes with Condition Expressions for more details。在

相关问题 更多 >

    热门问题