Header Ads

Header ADS

Update Data

 

DynamoDB তে data update করার syntax 


1️⃣ AWS CLI

aws dynamodb update-item \
  --table-name Users \
  --key '{"user_id": {"S": "1"}}' \
  --update-expression "SET age = :newAge, city = :newCity" \
  --expression-attribute-values '{":newAge":{"N":"25"}, ":newCity":{"S":"Chattogram"}}' \
  --endpoint-url http://localhost:8000

Explanation:

  1. --table-name → Table name

  2. --key → Update করার record এর Partition Key (Sort Key থাকলে দিতে হবে)

  3. --update-expression → কোন attribute update করতে হবে

  4. --expression-attribute-values → নতুন value define করে দেওয়া

  5. --endpoint-url → Local DynamoDB address

Optional: ConditionExpression use করলে update হবে শুধুমাত্র যদি কিছু condition satisfy হয়।


2️⃣ Python 

import boto3

dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000', region_name='us-east-1')
table = dynamodb.Table('Users')

response = table.update_item(
    Key={'user_id': '1'},  # Partition Key
    UpdateExpression="SET age = :newAge, city = :newCity",
    ExpressionAttributeValues={
        ':newAge': 25,
        ':newCity': 'Chattogram'
    },
    ReturnValues="UPDATED_NEW"  # Return only updated attributes
)

print("Updated attributes:", response['Attributes'])

Explanation:

  1. Key → Update করার record এর primary key

  2. UpdateExpression → কোন attribute update হবে, কীভাবে

  3. ExpressionAttributeValues → নতুন value mapping

  4. ReturnValues → updated attributes ফেরত পেতে


3️⃣ Notes 

  1. DynamoDB তে partial update possible → শুধু যেটা change করতে চাও, update হবে

  2. Numeric type increment/decrement করা সম্ভব:

table.update_item(
    Key={'user_id': '1'},
    UpdateExpression="SET age = age + :inc",
    ExpressionAttributeValues={':inc': 1},
    ReturnValues="UPDATED_NEW"
)
  1. ConditionExpression দিয়ে ensure করতে পারো update হবে শুধুমাত্র যদি কিছু শর্ত satisfy হয়:

table.update_item(
    Key={'user_id': '1'},
    UpdateExpression="SET city = :newCity",
    ExpressionAttributeValues={':newCity': 'Sylhet'},
    ConditionExpression="attribute_exists(user_id)",  # Only if record exists
    ReturnValues="UPDATED_NEW"
)

✅ Summary

OperationAWS CLIPython boto3Notes
Update dataupdate-itemupdate_item()Partial update possible, ReturnValues optional
Increment numericCLI + SET age = age + :incUpdateExpression="SET age = age + :inc"Auto increment
Conditional update--condition-expressionConditionExpression=...Safe update


Powered by Blogger.