Skip to content

Huiping27/dynamodb_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

I’ll use Python with the Boto3 library to interact with DynamoDB, and I’ll create a basic script that can perform CRUD (Create, Read, Update, Delete) operations.

Prerequisites AWS Account: AWS account to use DynamoDB. AWS CLI: Install and configure the AWS CLI with credentials. Python: Install Python (preferably 3.x). Boto3: Install the Boto3 library using pip (pip install boto3). Git: Install Git and create a GitHub account if you don’t have one.

Steps to Build the Application

  1. Set Up Environment Install Required Libraries:

pip install boto3 Configure AWS CLI (if not done already):

aws configure

  1. Create a Python Script Create a directory for your project and a file named dynamodb_app.py inside it.

mkdir dynamodb_project cd dynamodb_project touch dynamodb_app.py

  1. Write the Python Script Open dynamodb_app.py and add the following code:

python Copy code import boto3

Initialize a session using Amazon DynamoDB

dynamodb = boto3.resource('dynamodb', region_name='us-west-2')

Create DynamoDB table

def create_table(): table = dynamodb.create_table( TableName='Users', KeySchema=[ { 'AttributeName': 'username', 'KeyType': 'HASH' } ], AttributeDefinitions=[ { 'AttributeName': 'username', 'AttributeType': 'S' } ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } ) print("Table status:", table.table_status)

Add a user to the Users table

def add_user(username, first_name, last_name): table = dynamodb.Table('Users') table.put_item( Item={ 'username': username, 'first_name': first_name, 'last_name': last_name } ) print(f"Added user: {username}")

Get user details

def get_user(username): table = dynamodb.Table('Users') response = table.get_item( Key={ 'username': username } ) item = response.get('Item') print(item)

Update user details

def update_user(username, first_name, last_name): table = dynamodb.Table('Users') table.update_item( Key={ 'username': username }, UpdateExpression='SET first_name = :f, last_name = :l', ExpressionAttributeValues={ ':f': first_name, ':l': last_name } ) print(f"Updated user: {username}")

Delete user

def delete_user(username): table = dynamodb.Table('Users') table.delete_item( Key={ 'username': username } ) print(f"Deleted user: {username}")

if name == "main": create_table() add_user('jdoe', 'John', 'Doe') get_user('jdoe') update_user('jdoe', 'Johnny', 'Doe') get_user('jdoe') delete_user('jdoe') 4. Run the Script

python dynamodb_app.py This script will create a DynamoDB table called Users, add a user, retrieve the user’s details, update the user, and finally delete the user.

  1. Push the Project to GitHub Initialize Git Repository:

git init Add Files to the Repository:

git add . Commit the Changes:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages