Table Create
Table Creation In DynamoDB
1) SQL এ আমরা আগে database তৈরি করি, তারপর table তৈরি করি। DynamoDB তে পুরো কনসেপ্টটা different .
2) But DynamoDB তে
- Database layer নেই।
- DynamoDB তে তুমি table directly create করো, table নিজেই সব data hold করে।
Syntax :
টেবিল তৈরির সময় শুধু primary key টা বলে দিতে হয়। suppose একটা টেবিল এ name,id,mark আছে। আমি টেবিল তৈরির সময় শুধু id(primary key) টা বলবো। name,mark বলার দরকার নাই। ডাটা ইন্সার্ট করার সময় id , name , mark insert করলেই হবে। ডাটা ইন্সার্ট করার সময় যত মন তত ফিল্ড ইন্সার্ট করা যাবে
--------------------------------------------------------------------------------------------------------------------------------------
CLI Syntax – Minimum Required
aws dynamodb create-table \
--table-name <TableName> \
--attribute-definitions \
AttributeName=<PartitionKeyName>,AttributeType=<S|N|B> \
[AttributeName=<SortKeyName>,AttributeType=<S|N|B>] \
--key-schema \
AttributeName=<PartitionKeyName>,KeyType=HASH \
[AttributeName=<SortKeyName>,KeyType=RANGE] \
--billing-mode <PAY_PER_REQUEST|PROVISIONED> \
[--endpoint-url http://localhost:8000] # Only for local
2️⃣ Parameter Explanation
| Parameter | Description | Example |
|---|---|---|
--table-name | Table এর নাম | Users |
--attribute-definitions | Table এর key গুলোর type define করে | AttributeName=user_id,AttributeType=S |
--key-schema | Primary key define করে | AttributeName=user_id,KeyType=HASH |
--billing-mode | DynamoDB capacity | PAY_PER_REQUEST (on-demand), PROVISIONED |
--endpoint-url | Local development ক্ষেত্রে DynamoDB URL | http://localhost:8000 |
Notes:
S= String,N= Number,B= BinaryPartition Key = HASH
Sort Key = RANGE (Optional, যদি composite key ব্যবহার করা হয়)
3️⃣ Example – Simple Table (Partition Key Only)
aws dynamodb create-table \
--table-name Users \
--attribute-definitions AttributeName=user_id,AttributeType=S \
--key-schema AttributeName=user_id,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--endpoint-url http://localhost:8000
✅ এই table এখন Partition Key = user_id দিয়ে তৈরি হবে।
4️⃣ Example – Table with Partition Key + Sort Key
aws dynamodb create-table \
--table-name Orders \
--attribute-definitions \
AttributeName=order_id,AttributeType=S \
AttributeName=order_date,AttributeType=N \
--key-schema \
AttributeName=order_id,KeyType=HASH \
AttributeName=order_date,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST \
--endpoint-url http://localhost:8000
Partition Key =
order_idSort Key =
order_dateএই structure দিয়ে তুমি এক user এর multiple orders chronological order এ রাখতে পারবে
5️⃣ Optional Parameters (Professional Use)
| Parameter | Purpose |
|---|---|
--tags | Table tagging for environment/owner info |
--stream-specification | DynamoDB Streams ON/OFF |
--global-secondary-indexes | Extra query patterns support |
--local-secondary-indexes | Alternate sort key queries |
***
Key Schema/ key type এর মাদ্ধমে আমরা বলে দিই কোনটা partition key আর কোনটা short key .
Hash -> partition key
Range -> short key
Primary key = partition key বা partition key + short key
We can access the DynamoDB using Python
ধাপ ১: Python Environment প্রস্তুত করা
Python 3 ইনস্টল আছে ধরে নিচ্ছি।
Virtual environment তৈরি করা (recommended):
python3 -m venv dynamodb-env
source dynamodb-env/bin/activate
প্রয়োজনীয় Python library ইনস্টল করা:
pip install boto3
Explanation:
boto3 হলো AWS SDK Python-এর জন্য। এটা DynamoDB, S3, এবং অন্যান্য AWS services access করতে দেয়।
ধাপ ২: Python দিয়ে Local DynamoDB Connect করা
import boto3
# Local DynamoDB-এর জন্য client তৈরি
dynamodb = boto3.resource(
'dynamodb',
endpoint_url="http://localhost:8000", # Local DynamoDB URL
region_name="us-east-1", # region required even for local
aws_access_key_id="dummy", # Local এর জন্য dummy credentials
aws_secret_access_key="dummy"
)
এখানে
endpoint_urllocal docker container এর port নির্দেশ করছে।Local এর জন্য
aws_access_key_idএবংaws_secret_access_keyযেকোনো dummy value হতে পারে।
ধাপ ৩: Table তৈরি করা
table = dynamodb.create_table(
TableName='Users',
KeySchema=[
{
'AttributeName': 'userId',
'KeyType': 'HASH' # Partition Key
}
],
AttributeDefinitions=[
{
'AttributeName': 'userId',
'AttributeType': 'N' # Number type
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# Wait until table exists
table.meta.client.get_waiter('table_exists').wait(TableName='Users')
print("Table created successfully!")
table.meta.client.get_waiter('table_exists').wait(TableName='Products')
DynamoDB কিছু সময় নেয় table create করতে।এই waiter loop চালায়, periodically check করে table তৈরি হয়েছে কি না, এবং table ready হলে execution continue হয়।
“Products table তৈরি হচ্ছে। যতক্ষণ না table completely ready, execution hold করো। table ready হলে পরবর্তী line execute হবে।”
যদি তুমি waiter না ব্যবহার করো, তখন তুমি table এ immediately access করার চেষ্টা করলে error আসতে পারে কারণ table এখনও fully ready হয়নি।