Skip to content

Instantly share code, notes, and snippets.

@bahrmichael
Last active January 5, 2021 17:55
Show Gist options
  • Save bahrmichael/f0a8807706232ee0794d5f4e3ab862b4 to your computer and use it in GitHub Desktop.
Save bahrmichael/f0a8807706232ee0794d5f4e3ab862b4 to your computer and use it in GitHub Desktop.
import boto3
from uuid import uuid4
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('random-table')
# Create 100 records with a random partition key
for i in range(100):
item = {'pk': str(uuid4()), 'text': f"{i}"}
table.put_item(Item=item)
print(f"Inserted {item}")
# Read 3 records and print them with the consumed capacity
for i in range(3):
response = table.scan(
Limit=1,
ExclusiveStartKey={
'pk': 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