DynamoDB is a database service provided and managed by AWS. DynamoDB is a serverless database completely optimized and managed by AWS. It is really helpful when we need to work with big data or we just need to dump the data in the database.
DynamoDB is a completely NoSQL database that provides independent tables instead of a complete database. Being NoSQL means, we do not need to define and maintain the structure of the data before or after the storage. But still, many limitations make us analyze the requirement before using DynamoDB as the primary database.
DynamoDB basic actions
When working on DynamoDB we can only run queries on a single table rather than aggregating multiple tables. This is one of the limitations we need to keep in mind when working with DynamoDB. We can run 4 types of operations on DynamoDB, i.e. Create, Update, Delete, Fetch.
First, we start by creating the table. This can be done either from the AWS console or using the aws cli.
Create a table
aws dynamodb create-table \
--table-name Art \
--attribute-definitions \
AttributeName=Artist,AttributeType=S \
AttributeName=ArtTitle,AttributeType=S \
--key-schema \
AttributeName=Artist,KeyType=HASH \
AttributeName=ArtTitle,KeyType=RANGE \
--provisioned-throughput \
ReadCapacityUnits=10,WriteCapacityUnits=5
Above we are creating a new table named Art
. It has two attributes, Artist as Hash key and Art Title as Range key.
Write item to a table
aws dynamodb put-item \ --table-name Art \ --item file://arts.json
Read items from a table
aws dynamodb get-item \ --table-name Art \ --item file://arts.json
Delete item from a table
aws dynamodb delete-item --table-name Art --key file://key.json
Query a table
aws dynamodb query --table-name Art
--key-condition-expression "Artist=:Artist and ArtTitle=:Songtitle"
There are two ways in which we can fetch the data from DynamoDB.
- Scan:
A Scan operation returns one or more items or item attributes by accessing every item in a table or a secondary index. Scan operations are generally slower and more expensive. Scan operations proceed sequentially. Applications can request a parallel Scan operation for faster performance on a large table or secondary index.
- Query:
A Query operation finds items based on primary key values. Query operations are designed to be query-optimized. Query operations support a subset of comparison operators on key attribute values. They only support an equal operator evaluation of the Primary Key.
Also, learn about Creating serverless flask application from scratch.