Last active
January 5, 2021 17:55
-
-
Save bahrmichael/a761d7d90d5f7d0c55f87cd5de0f7288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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