Header Ads

Header ADS

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

ParameterDescriptionExample
--table-nameTable এর নামUsers
--attribute-definitionsTable এর key গুলোর type define করেAttributeName=user_id,AttributeType=S
--key-schemaPrimary key define করেAttributeName=user_id,KeyType=HASH
--billing-modeDynamoDB capacityPAY_PER_REQUEST (on-demand), PROVISIONED
--endpoint-urlLocal development ক্ষেত্রে DynamoDB URLhttp://localhost:8000

Notes:

  1. S = String, N = Number, B = Binary

  2. Partition Key = HASH

  3. 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
  1. Partition Key = order_id

  2. Sort Key = order_date

  3. এই structure দিয়ে তুমি এক user এর multiple orders chronological order এ রাখতে পারবে


5️⃣ Optional Parameters (Professional Use)

ParameterPurpose
--tagsTable tagging for environment/owner info
--stream-specificationDynamoDB Streams ON/OFF
--global-secondary-indexesExtra query patterns support
--local-secondary-indexesAlternate 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 প্রস্তুত করা

  1. Python 3 ইনস্টল আছে ধরে নিচ্ছি।

  2. Virtual environment তৈরি করা (recommended):

python3 -m venv dynamodb-env
source dynamodb-env/bin/activate
  1. প্রয়োজনীয় 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_url local 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 হয়নি।

 

 

 

 

 

 

Powered by Blogger.