Data Insert In DynamoDB
DynamoDB তে data insert করার syntax
1️⃣ AWS CLI style (DynamoDB syntax) – CLI ব্যবহার হলে বা API call style
2️⃣ Python boto3 style – SDK দিয়ে, CLI ছাড়াই
1️⃣ DynamoDB CLI (Conceptual Syntax)
aws dynamodb put-item \
--table-name TableName \
--item '{
"PartitionKeyName": {"S": "value"},
"SortKeyName": {"S": "value"}, # যদি table এ থাকে
"attribute1": {"S": "value"},
"attribute2": {"N": "123"},
"attribute3": {"BOOL": true}
}' \
--endpoint-url http://localhost:8000
Explanation:
--table-name→ তোমার table এর নাম--item→ JSON format, যেখানে DynamoDB type explicitly দিতে হয় (S,N,BOOL,M,L)--endpoint-url→ Local Docker DynamoDB এর জন্য
Example: Users Table (PK only)
aws dynamodb put-item \
--table-name Users \
--item '{
"user_id": {"S": "1"},
"name": {"S": "Ali"},
"age": {"N": "24"},
"city": {"S": "Dhaka"}
}' \
--endpoint-url http://localhost:8000
2️⃣ Python boto3 syntax (Recommended CLI-less method)
import boto3
# Connect to local DynamoDB
dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000', region_name='us-east-1')
# Table object
table = dynamodb.Table('Users')
# Insert / put item
table.put_item(
Item={
'user_id': '1', # Partition Key
'name': 'Ali',
'age': 24, # Number type
'city': 'Dhaka'
}
)
print("Data inserted successfully!")
Notes:
Python dict automatically DynamoDB type map করে (
str → S,int → N,bool → BOOL)Partition Key অবশ্যই দিতে হবে, Sort Key যদি থাকে, সেটাও দিতে হবে
put_itemalready existing primary key replace করে (unless ConditionExpression use করা হয়)
✅ Comparison Table
| Feature | CLI / JSON | Python boto3 |
|---|---|---|
| Syntax style | JSON + explicit DynamoDB types | Python dict, types auto-convert |
| Local Docker support | --endpoint-url http://localhost:8000 | endpoint_url='http://localhost:8000' |
| Condition / advanced | --condition-expression | ConditionExpression=... |
| Ease of use | Verbose, manual typing | Cleaner, easy for scripts & automation |
Insert Multiple data/row/item at a time :
1️⃣ Python boto3 – Batch Insert
import boto3
# Connect to local DynamoDB
dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000', region_name='us-east-1')
table = dynamodb.Table('Users')
# Multiple records
items = [
{'user_id': '1', 'name': 'Ali', 'age': 24, 'city': 'Dhaka'},
{'user_id': '2', 'name': 'Sara', 'age': 22, 'city': 'Chattogram'},
{'user_id': '3', 'name': 'Rafi', 'age': 28, 'city': 'Khulna'},
{'user_id': '4', 'name': 'Mina', 'age': 26, 'city': 'Sylhet'},
{'user_id': '5', 'name': 'Tanvir', 'age': 30, 'city': 'Barishal'}
]
# Batch insert
with table.batch_writer() as batch:
for item in items:
batch.put_item(Item=item)
print("All records inserted successfully!")
✅ Key Points:
batch_writer()use করলে DynamoDB automatically batch operation handle করে।Up to 25 items per batch (DynamoDB limitation)
2️⃣ AWS CLI
AWS CLI হলে batch-write-item use করা হয়। Example:
aws dynamodb batch-write-item \
--request-items '{
"Users": [
{"PutRequest": {"Item": {"user_id":{"S":"1"}, "name":{"S":"Ali"}, "age":{"N":"24"}, "city":{"S":"Dhaka"}}}},
{"PutRequest": {"Item": {"user_id":{"S":"2"}, "name":{"S":"Sara"}, "age":{"N":"22"}, "city":{"S":"Chattogram"}}}},
{"PutRequest": {"Item": {"user_id":{"S":"3"}, "name":{"S":"Rafi"}, "age":{"N":"28"}, "city":{"S":"Khulna"}}}}
]
}' \
--endpoint-url http://localhost:8000