AWS DynamoDB basics
What is AWS DynamoDB?
Section titled “What is AWS DynamoDB?”AWS DynamoDB is a fully managed NoSQL database service, It’s designed to handle key-value and document data models, making it an ideal choice for applications that require low-latency and scalability.
DynamoDB is serverless, meaning we don’t have to manage the underlying infrastructure. It automatically scales up or down based on our workload and provides high availability through data replication.
Main Concepts
Section titled “Main Concepts”Tables
Section titled “Tables”Data in DynamoDB is stored and grouped as tables, at table contains items (rows) and attributes (columns).
Each item represents a single record in the table, an item is uniquely identified by its primary key.
Primary Key
Section titled “Primary Key”Data in DynamoDB is stored physically in partitions, A Primary key determines where data is stored. we have 2 types of keys:
- Partition Key: A single attribute used to distribute data.
- Composite Key: A combination of partition key and sort key.
Attributes
Section titled “Attributes”Attributes represent data elements within an item, it can be of types like string, number, boolean, list, or map.
Provisioned and On-Demand Capacity
Section titled “Provisioned and On-Demand Capacity”Using DynamoDB, we pay per read and write capacity units per second, Capacity units can scale based on our setup to handle more traffic. we 2 types of capacities:
- Provisioned: We specify read/write capacity units.
- On-Demand: DynamoDB adjusts capacity automatically based on traffic.
Indexes
Section titled “Indexes”to query data more flexibly and faster we us indexes, there 2 types of Indexes in DynamoDB:
- Local Secondary Index (LSI): Supports the same partition key but a different sort key.
- Global Secondary Index (GSI): Supports different partition and sort keys.
Working with DynamoDB
Section titled “Working with DynamoDB”Step 1: Install the AWS CLI
Section titled “Step 1: Install the AWS CLI”Make sure we have the AWS CLI installed and configured with our credentials.
Step 2: Create a DynamoDB Table
Section titled “Step 2: Create a DynamoDB Table”Let’s create a table named Movies with a partition key MovieID (string).
aws dynamodb create-table \ --table-name Movies \ --attribute-definitions AttributeName=MovieID,AttributeType=S \ --key-schema AttributeName=MovieID,KeyType=HASH \ --billing-mode PAY_PER_REQUESTIn this command we have:
- Defined
MovieIDas the partition key. - Used the Pay-Per-Request billing mode.
Step 3: Insert Data into the Table
Section titled “Step 3: Insert Data into the Table”Let’s Add an item to the Movies table:
aws dynamodb put-item \ --table-name Movies \ --item '{"MovieID": {"S": "001"}, "Title": {"S": "Inception"}, "Genre": {"S": "Sci-Fi"}}'Step 4: Query Data
Section titled “Step 4: Query Data”To retrieve the item for the movie we have created:
aws dynamodb get-item \ --table-name Movies \ --key '{"MovieID": {"S": "001"}}'Step 5: Update an Item
Section titled “Step 5: Update an Item”We can Update an item in a DynamoDB table, for eg. let’s update the Genre of the Movie we have just created:
aws dynamodb update-item \ --table-name Movies \ --key '{"MovieID": {"S": "001"}}' \ --update-expression "SET Genre = :g" \ --expression-attribute-values '{":g": {"S": "Action"}}'Step 6: Delete an Item
Section titled “Step 6: Delete an Item”Let’s Remove the item movie with MovieID = 001:
aws dynamodb delete-item \ --table-name Movies \ --key '{"MovieID": {"S": "001"}}'Step 7: Delete the Table
Section titled “Step 7: Delete the Table”At the end, let’s clean up by deleting the table:
aws dynamodb delete-table \ --table-name MoviesConclusion
Section titled “Conclusion”In this tutorial, we understood what DynamoDB is and its main features, we Explored key concepts like tables, items, and primary keys. we performed basic operations like creating a table, inserting data, querying, updating, and deleting items and tables. We’re now equipped to handle DynamoDB’s core functionality to build applications.