Header Ads

Header ADS

DynamoDB Read data

 

DynamoDB থেকে data read 

1️⃣ AWS CLI 

DynamoDB তে data read করার জন্য প্রধানত 3  operation আছে:

  1. GetItem → Primary key দিয়ে এক record fetch

  2. Query → Partition Key (and optionally Sort Key) দিয়ে multiple records fetch

  3. Scan → Table এর সব record fetch (expensive, use carefully)


1.1 GetItem

aws dynamodb get-item \
  --table-name Users \
  --key '{"user_id": {"S": "1"}}' \
  --endpoint-url http://localhost:8000

Explanation:

  1. --table-name → Table name

  2. --key → Primary key in JSON format (Partition + Sort key if applicable)

  3. --endpoint-url → Local Docker DynamoDB


1.2 Query (Partition Key)

aws dynamodb query \
  --table-name Users \
  --key-condition-expression "user_id = :uid" \
  --expression-attribute-values '{":uid":{"S":"1"}}' \
  --endpoint-url http://localhost:8000

Query multiple items with same Partition Key (and optional Sort Key)

Returns items in a list


1.3 Scan (Full table read)

aws dynamodb scan \
  --table-name Users \
  --endpoint-url http://localhost:8000

Table এর সব rows return করবে

Large table হলে expensive → avoid in production


2️⃣ Python 

Python boto3 দিয়ে read operations অনেক সহজ:


2.1 GetItem

import boto3

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

response = table.get_item(
    Key={'user_id': '1'}
)

item = response.get('Item')
print(item)

Partition Key + Sort Key (if any) দিতে হবে

Returns dict object


2.2 Query (Partition Key + optional Sort Key)

from boto3.dynamodb.conditions import Key

response = table.query(
    KeyConditionExpression=Key('user_id').eq('1')
)

items = response['Items']
print(items)

KeyConditionExpression দিয়ে filter by primary key

Returns list of items


2.3 Scan (Full table read)

response = table.scan()
items = response['Items']
print(items)

Table এর সব rows return করে

Large table এ performance cost বেশি


✅ Summary / Recommendation

OperationAWS CLIPython boto3Use case
Single recordget-itemget_item()Primary key দিয়ে fetch
Multiple records by PKqueryquery()Partition Key + optional Sort Key
Full tablescanscan()Debug / small table

💡 Pro Tip:

Query always preferred over Scan (fast & cheap)

Python boto3 gives more control + CLI not required



read operations with multiple filter 



Powered by Blogger.