DynamoDB Script Example
Python Script for Populating DynamoDB Game Scores
This Python script demonstrates how to use the boto3
library to interact with Amazon DynamoDB. It's designed to simulate populating a gamescores
table with data for various users and gaming events. This example is particularly useful for developers testing DynamoDB integrations locally using tools like LocalStack or for understanding basic data insertion patterns.
Script Overview
The script initializes a DynamoDB client, defines dictionaries for user data and a list of gaming events. It then iterates through each event and each user, generating a unique item for each combination. Each item includes details such as event name, timestamp, gamer ID, name, age, location, game played, a randomly generated score, and the user's rank. The put_item
operation is used to insert these records into the specified DynamoDB table. A delay is introduced between processing each event to simulate real-world data flow and avoid overwhelming the database.
Key Features and Usage
- Boto3 Integration: Leverages the official AWS SDK for Python to connect to DynamoDB.
- Local DynamoDB Support: Configured to connect to a local endpoint (e.g.,
http://localhost:4567
), making it ideal for development and testing without incurring AWS costs. - Data Simulation: Generates realistic-looking user profiles and event data for a gaming tournament scenario.
- Random Score Generation: Assigns random scores within a defined range to simulate competitive outcomes.
- Timestamping: Records the UTC timestamp for each score submission.
- Structured Data: Organizes data into a dictionary format suitable for DynamoDB item insertion.
Code Snippet
import boto3
import time
import random
import datetime
# Initialize DynamoDB client for a specific region, using provided credentials and local endpoint
client = boto3.Session(region_name='eu-west-1').client('dynamodb', aws_access_key_id='', aws_secret_access_key='', endpoint_url='http://localhost:4567')
# Define user data with unique IDs and attributes
userlists = {}
userlists['john'] = {'id':'johnsnow9801', 'firstname': 'john', 'age': '23', 'location': 'south africa', 'rank': 'professional'}
userlists['max'] = {'id':'maxmilia', 'firstname': 'max', 'age': '24', 'location': 'new zealand', 'rank': 'professional'}
userlists['samantha'] = {'id':'sambubbles8343', 'firstname': 'samantha', 'age': '21', 'location': 'australia', 'rank': 'professional'}
userlists['aubrey'] = {'id':'aubreyxeleven4712', 'firstname': 'aubrey', 'age': '24', 'location': 'america', 'rank': 'professional'}
userlists['mikhayla'] = {'id':'mikkie1419', 'firstname': 'mikhayla', 'age': '21', 'location': 'mexico', 'rank': 'professional'}
userlists['steve'] = {'id':'stevie1119', 'firstname': 'steve', 'age': '25', 'location': 'ireland', 'rank': 'professional'}
userlists['rick'] = {'id':'rickmax0901', 'firstname': 'rick', 'age': '20', 'location': 'sweden', 'rank': 'professional'}
userlists['michael'] = {'id':'mikeshank2849', 'firstname': 'michael', 'age': '26', 'location': 'america', 'rank': 'professional'}
userlists['paul'] = {'id':'paulgru2039', 'firstname': 'paul', 'age': '26', 'location': 'sweden', 'rank': 'professional'}
userlists['nathalie'] = {'id':'natscotia2309', 'firstname': 'nathalie', 'age': '21', 'location': 'america', 'rank': 'professional'}
userlists['scott'] = {'id':'scottie2379', 'firstname': 'scott', 'age': '23', 'location': 'new zealand', 'rank': 'professional'}
userlists['will'] = {'id':'wilson9335', 'firstname': 'will', 'age': '27', 'location': 'sweden', 'rank': 'professional'}
userlists['adrian'] = {'id':'adriano5519', 'firstname': 'adrian', 'age': '22', 'location': 'ireland', 'rank': 'professional'}
userlists['julian'] = {'id':'jules8756', 'firstname': 'julian', 'age': '27', 'location': 'mexico', 'rank': 'professional'}
userlists['rico'] = {'id':'ricololo4981', 'firstname': 'rico', 'age': '20', 'location': 'sweden', 'rank': 'professional'}
userlists['kate'] = {'id':'kitkatkate0189', 'firstname': 'kate', 'age': '24', 'location': 'south africa', 'rank': 'professional'}
# Define a list of gaming events with their names and associated games
events = []
events = [
{
'name': 'gaming_nationals_round_01',
'game': 'counter_strike'
},
{
'name': 'gaming_nationals_round_02',
'game': 'fifa'
},
{
'name': 'gaming_nationals_round_03',
'game': 'rocket_league'
},
{
'name': 'gaming_nationals_round_04',
'game': 'world_of_warcraft'
},
{
'name': 'gaming_nationals_round_05',
'game': 'pubg'
},
{
'name': 'gaming_nationals_round_06',
'game': 'league_of_legends'
},
{
'name': 'gaming_nationals_round_07',
'game': 'dota'
}
]
# Get a list of user keys from the userlists dictionary
users = userlists.keys()
# Function to generate a DynamoDB item for a user and an event
def generate(name, eventname):
item = {
'event': {'S': eventname['name']}, # Event name as String
'timestamp': {'S': datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M")}, # Current UTC time formatted as String
'gamerid': {'S': name['id']}, # User's unique ID as String
'name': {'S': name['firstname']}, # User's first name as String
'age': {'N': str(name['age'])}, # User's age as Number (converted to string for 'N' type)
'location': {'S': name['location']}, # User's location as String
'game': {'S': eventname['game']}, # Game associated with the event as String
'score': {'N': str(random.randint(10000, 19999))}, # Random score between 10000 and 19999 as Number
'rank': {'S': name['rank']}} # User's rank as String
return item
# Iterate through each event and each user to populate DynamoDB
for eventname in events:
for user in users:
item = generate(userlists[user], eventname)
print("Event: {} - submitting scores to dynamodb for {}".format(item['event']['S'], user))
# Use put_item to insert the generated item into the 'gamescores' table
response = client.put_item(TableName='gamescores', Item=item)
# Pause for 5 minutes (300 seconds) between processing each event
time.sleep(300)
print("")
print("done")