amazon-dynamodb - 如何使用 boto3 更新 dynamodb 中的密钥?
问题描述
我是 DynamoDB 的新手,在定义表时遇到了麻烦。我想要一张这样的桌子:
项目名称、创建日期、更新日期
我需要查询和更新具有特定 item_name 和 update_date 的项目。所以在 boto3 我写了
table.update_item(
Key={
'item_name': item_name,
'update_date': 0
},
UpdateExpression='SET update_date = :val1',
ExpressionAttributeValues={
':val1': 999999999
}
)
我明白了botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: One or more parameter values were invalid: Cannot update attribute update_date. This attribute is part of the key
。
在我的表定义中,我有item_name
哈希键和update_date
范围键。我将如何修复定义或代码,以便
- 更新操作成功。
- 我可以快速查询表中的
item_name
和update_date
属性吗?
解决方案
1)首先updated_date是你主键的一部分,这意味着dynamodb中的主键由两个属性组成,Hash(item_name)和Range(updated_date)主键,你不能主键。
2)如果你想更新 updated_date 属性,那么你应该更新你的表配置并选择 item_name only 作为主键
推荐阅读
- python - 如何使用 scikit-learn 中的新示例训练训练有素的模型?
- yaml - 如何在 ci.yml 文件中的“when:”中嵌套“except:”
- c++ - 如何处理模板类标头中的循环 #include 调用?
- c - C 中这个特定的 IF 语句实际上做了什么(这也有效)?
- python - 用 Python 填充缺失的数据
- python - 了解包导入
- npm - 如何修复“找不到模块:无法解析 x”
- java - 如何修复 maven 打包的 jar 报告“错误:无法找到或加载主类 .\FormulaTelemetryApp-1.0-SNAPSHOT.jar”
- apache-zookeeper - zookeeper - [NIOServerCnxn@383] - 导致会话 0x0 关闭的异常:Len 错误 1195725856
- javascript - 使用 Google Chrome 浏览器控制台上的 Quora 上的所有问题一次性回答所有请求