2017-12-17 9 views
0

장치는 내 파티션 키입니다. 테이블은 동일한 장치 아래에 여러 다른 사용자를 배치하기위한 것입니다. 그러나 다음과 같은 put_item() 코드를 실행하면 Device 키가 동일한 경우 각 사용자를 덮어 씁니다.Dynamodb put_item()이 데이터를 덮어 씁니다.

예 : 내 device 변수에 Monitor을 입력하고 gomez 변수를 aliasInput 변수로 실행합니다. 그런 다음 으로 다시 실행하십시오. 내 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, 
+1

당신은 당신이 결과로 볼 원하는 작업의 샘플 출력을 제공시겠습니까? '모니터'장치의 여러 행 또는 여러 개의 'aliasInput'이있는 하나의 레코드? – GSazheniuk

답변

1

당신은 update_item() 찾고 있습니다. 당신은 AttributeUpdates 때문에 UpdateExpression가되지 않습니다 사용해야하지만이 간단한 예제는 시작해야합니다

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

추가하는 대신 항목을 대체하는 위치에 여전히 문제가 있습니다. –