Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dynamodb put_item()覆盖数据_Python_Amazon Web Services_Amazon Dynamodb - Fatal编程技术网

Python Dynamodb put_item()覆盖数据

Python Dynamodb put_item()覆盖数据,python,amazon-web-services,amazon-dynamodb,Python,Amazon Web Services,Amazon Dynamodb,设备是我的分区键,表用于在同一设备下输入多个不同的用户。但是,如果我运行下面的put_item()代码,如果每个用户具有相同的设备密钥,它将覆盖这些用户 示例:如果我输入监视器,作为我的设备变量,并将戈麦斯作为我的别名输入变量运行。 然后将其作为监视器再次作为我的设备变量运行,但作为我的别名输入将覆盖我的戈麦斯条目 将数据输入我的表格的函数: import boto3 import json import decimal import time import datetime # Helper

设备是我的分区键,表用于在同一设备下输入多个不同的用户。但是,如果我运行下面的put_item()代码,如果每个用户具有相同的设备密钥,它将覆盖这些用户

示例:如果我输入
监视器
,作为我的
设备
变量,并将
戈麦斯
作为我的
别名输入
变量运行。 然后将其作为
监视器
再次作为我的
设备
变量运行,但作为我的
别名输入
将覆盖我的
戈麦斯
条目

将数据输入我的表格的函数:

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, 
你在找我。您应该使用
UpdateExpression
,因为
AttributeUpdates
已被弃用,但这个简单的示例应该让您开始:

response = table.update_item(
   Key={
     'Device': device,
   },
   AttributeUpdates={
     'RequestList': {
       'Alias': aliasInput,
       'Date': date
     },
     'AvailableQuanity': 0,
     'ReserveQuanity': 0,
   },
)
放置项目

创建新项目,或用新项目替换旧项目。如果指定表中已存在与新项具有相同主键的项,则新项将完全替换现有项

要防止覆盖,您需要添加一个 指定分区密钥尚不存在

类似于下面的内容应该可以工作(对不起,我没有完全了解您的密钥方案,因此您必须修改它)

table.put\u项目(
项={'userId':1,'productId':2},
ConditionExpression='userId:uid和productId:pid',
ExpressionAttributeValues={:uid':1':pid':3}
)

能否提供您希望看到结果的示例输出?“监视器”设备有多行,或一条记录有多个“别名输入”?我仍然有一个问题,它仍然在替换项目而不是添加。我有同样的问题,出于某种原因,这对我不起作用…属性\u也不存在。我只是想防止覆盖相同的哈希分区键!如果由于该情况而未发生写入,是否仍计入写入容量单位?
table.put_item(
    Item={'userId': 1, 'productId': 2}, 
    ConditionExpression='userId <> :uid AND productId <> :pid', 
    ExpressionAttributeValues={':uid': 1, ':pid': 3}
)