Skip to content

Instantly share code, notes, and snippets.

@bahrmichael
Last active January 5, 2021 17:55
Show Gist options
  • Save bahrmichael/a761d7d90d5f7d0c55f87cd5de0f7288 to your computer and use it in GitHub Desktop.
Save bahrmichael/a761d7d90d5f7d0c55f87cd5de0f7288 to your computer and use it in GitHub Desktop.
import boto3
from uuid import uuid4
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('random-table-categorized')
categories = ['easy', 'medium', 'difficult']
for category in categories:
# Create 50 records for each category with a random sort key
for i in range(50):
item = {'pk': category, 'sk': str(uuid4()), 'text': f"{category}-{i}"}
table.put_item(Item=item)
print(f"Inserted {item}")
for category in categories:
# Read 3 records and print them with the consumed capacity
for i in range(3):
response = table.query(
Limit=1,
KeyConditionExpression=Key('pk').eq(category) & Key('sk').gt(str(uuid4())),
ReturnConsumedCapacity='TOTAL'
)
if response['Items']:
print({
"Item": response['Items'][0],
"Capacity": response['ConsumedCapacity']['CapacityUnits'],
"ScannedCount": response['ScannedCount']
})
else:
print("Didn't find an item. Please try again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment