DynamoDb is a fully managed NoSQL database service provided by Amazon Web Services. It is a highly available and durable service with strong guarantees from AWS. It automatically scales tables up and down to serve the request traffic.
Tables are a collection of items. There is no limit on the number of items that a table can store.
Items are a collection of attributes. Each item can be uniquely identified.
Attributes in DynamoDB are the smallest unit of data in DynamoDB. They are similar to fields in other relational database systems.
For the purpose of this article, we will be considering the following Books table schema:
Title: The title of the book
Author: The author of the book
Category: The category of the book
Description: The description of the book
Partition Key: Author
Sort Key: Title
There is a terminal widget at the end of this Answer, where these commands can be tried out. Add the AWS access key, secret key, region and output to the environment. The output in our case will be type JSON. Once the keys are added, copy and paste the command snippets to perform the actions on DynamoDB.
aws dynamodb create-table \--table-name Books \--attribute-definitions \AttributeName=Title,AttributeType=S \AttributeName=Author,AttributeType=S \--key-schema \AttributeName=Author,KeyType=HASH \AttributeName=Title,KeyType=RANGE \--provisioned-throughput \ReadCapacityUnits=5,WriteCapacityUnits=5
The create-table
command takes as input the table-name
as defined on line 2.
The attribute-definitions
defines the attributes of the table. It expects the name and the type of the attribute to be declared.
The key-schema
defines the keys on which the data would be indexed. The HASH
key defines the partition key and the RANGE
key defines the sort
key for DynamoDB.
The provisioned-throughput
defines the throughput for the table. It is an integer that defines the maximum number of reads/writes consumed per second. Beyond this number DynamoDB returns ThrottlingException
.
The output of the create command shows the TableStatus
as CREATING
. You can use the describe-table
to verify that the table creation has happened.
aws dynamodb describe-table --table-name Books | grep TableStatus
The output of describe-table
command shows the TableStatus
as ACTIVE
.
aws dynamodb put-item \--table-name Books \--item \'{"Author": {"S": "Martin Kleppmann"}, "Title": {"S": "Designing Data-Intensive Applications"}, "Category": {"S": "Data Engineering"}, "Description": {"S": "Data is at the center of many challenges in system design today. Difficult issues need to be figured out, such as scalability, consistency, reliability, efficiency, and maintainability"}}'aws dynamodb put-item \--table-name Books \--item \'{"Author": {"S": "Scott Meyers"}, "Title": {"S": "Effective Modern C++"}, "Category": {"S": "C++"}, "Description": {"S": "The challenge is learning to use those features effectively—so that your software is correct, efficient, maintainable, and portable."}}'aws dynamodb put-item \--table-name Books \--item \'{"Author": {"S": "Alex Petrov"}, "Title": {"S": "Database Internals"}, "Category": {"S": "Relational Databases"}, "Description": {"S": "When it comes to choosing, using, and maintaining a database, understanding its internals is essential. But with so many distributed databases and tools available today, it’s often difficult to understand what each one offers and how they differ."}}'aws dynamodb put-item \--table-name Books \--item \'{"Author": {"S": "Travis Jeffery"}, "Title": {"S": "Distributed Services with Go"}, "Category": {"S": "Go"}, "Description": {"S": "Data is at the center of many challenges in system design today. Difficult issues need to be figured out, such as scalability, consistency, reliability, efficiency, and maintainability"}}'
Use the scan command for listing all the items in the table. This should not be used for large datasets.
aws dynamodb scan --table-name Books
For deleting a single item, use the following command—both the keys provided in the schema have to be provided.
aws dynamodb delete-item \--table-name Books \--key '{"Author" : {"S": "Travis Jeffery"},"Title" : {"S": "Distributed Services with Go"}}'
For deleting all the items in the DynamoDB table, the fastest method is to delete the table and recreate it. To do this, the following commands can be used.
aws dynamodb delete-table --table-name Booksaws dynamodb create-table \--table-name Books \--attribute-definitions \AttributeName=Title,AttributeType=S \AttributeName=Author,AttributeType=S \--key-schema \AttributeName=Author,KeyType=HASH \AttributeName=Title,KeyType=RANGE \--provisioned-throughput \ReadCapacityUnits=5,WriteCapacityUnits=5
To test run these commands in the terminal widget below, please enter your AWS access key in the
aws_access_key_id
value and your AWS secret key in theaws_secret_access_key
value before running the terminal.