Device是我的分区键,table用于在同一个设备下放置多个不同的用户。但是,如果我运行下面的put_item()代码,它将覆盖每个拥有相同设备密钥的用户。在
示例:如果我放入Monitor
,作为我的device
变量,将gomez
作为我的aliasInput
变量运行。
然后以Monitor
的形式再次运行它,再次作为我的device
变量运行,但是craig
作为我的aliasInput
它覆盖了我的gomez
条目。在
函数将数据输入到我的表中:
import boto3
import json
import decimal
import time
import datetime
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")
table = dynamodb.Table('WishListTest')
device = input('What is the Item being requested?\n')
device = device.upper()
aliasInput = input('What is the Alias of the user?\n')
aliasInput = aliasInput.upper()
date = int((time.strftime("%d%m%Y")))
response = table.put_item(
Item={
'Device': device,
'RequestList': {
'Alias': aliasInput,
'Date': date
},
'AvailableQuanity': 0,
'ReserveQuanity': 0,
}
)
print("PutItem succeeded:")
print(json.dumps(response,
来自the docs:放置项目
要防止覆盖,您需要添加conditional expression 指定分区键尚不存在。在
下面这样的方法应该可以工作(抱歉,我没有完全了解您的密钥方案,因此您必须修改此方案)。在
你在找^{} 。您应该使用
UpdateExpression
,因为AttributeUpdates
已被弃用,但是这个简单的示例应该可以让您开始:相关问题 更多 >
编程相关推荐