ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

DynamoDB – Create and Query NoSQL Database on AWS

DynamoDB – Create and Query NoSQL Database on AWS
DynamoDB – Create and Query NoSQL Database on AWS

In this article, we’ll learn to create the DynamoDB NoSQL database on AWS as an Amazon DynamoDB, add records using dynamic schema, and query the Amazon DynamoDB table. DynamoDB is a NoSQL database on Amazon which has a flexible schema so that we can have any no. of attributes for each row at any point in time.

NoSQL refers to a non-SQL or a non-relational database that provides a mechanism for the storage and retrieval of data modeled in other than tabular relations(unlike relational databases).

Amazon DynamoDB is a key-value and document database with a high level of performance(single-digit milliseconds). Tables in DynamoDB tables support two types of primary keys:

  • partition key only
  • partition key and sort key

When querying for the table, we must provide the partition key value and apply the condition to the sort key values so that we can retrieve specific data. Due to flexible schema, we can add a new attribute to a specific item after it has been already created.

Now, let’s dive into how to create the first DynamoDB NoSQL database and query the table:

In this, we’ll:

  • Create a NoSQL database (DynamoDB)
  • Add records using dynamic schema
  • Query a table

Prerequisites:

  • AWS account
  • Basic NoSQL concepts

First DynamoDB NoSQL Database on AWS

Creating

Step-1:

  • Go to AWS console.
  • In the services search bar, type dynamodb
  • Click on DynamoDB.
  • Now, click on Create table.

Amazon DynamoDB supports both key-value and document data models. This means it’s flexible and each row can have any number of columns at any point in time.

  • Type table name. Under Partition key type: userId. (You must type the partition key exactly as shown userId)
  • Choose string data type.
  • Under Sort key type: lastDateWatched. Choose data type as Number from the drop-down.

When creating the table, you must specify the primary key of the table.

  • Now, Accept the default settings. And click on Create table.

Step-2:

  • Once status changes to Active, click the table name.

DynamoDB has a composite key that uses both a partition and a sort key. In a table, if it has both partition and sort key, it’s possible for two items to have the same partition key-value. But those two must have different sort key values.

  • Select the table name and click on Actions drop-down button then choose Create item.
  • Under userId Attrubute name, type: 1234-5678-abcd-efgh
  • Under lastDateWatched, type: 20220601 (Note: this is an example, and it’s a UNIX timestamp)
  • Click on Add new attribute. Choose String and for the new attribute name, type: clipId with value 9876-5432-zyxw-vuts (This is an example value)
  • Similarly, create other items including list data types too as shown in the picture below. And under list data type, insert some fields. (eg. deviceType: Cloudy Fox TV, Cloudy Fox Tablet)
  • Click on Create item.
  • Now, on the left menu, click Explore items.
  • Click userId 1234-5678-abcd-efgh to edit the record.
  • Click on Add new attribute drop-down and add another attribute. (eg. lastStopTime: 80)
  • Then click on Save changes.

After creating a record you can still edit it.

DynamoDB is schemaless, which means you can add attributes to the table for any record. You can add a new attribute, in this case lastStopTime attribute with a number data type that stores the total time in seconds for clip viewing.


Also Read : Crash Course on MongoDB


Querying

Steps:

  • Click on Scan/Query items.
  • Select Query tab.
  • Under userId, type: 1234-5678-abcd-efgh
  • Under lastDateWatched, select Greater than and put value: 20220601
  • Click Run.
  • Check the records returned.

To change the query criteria,

  • Type different userId and review the results below. Nothing will be returned since there are no records matching.

Now, select the Scan tab and click Run. It will display all the records on the table.

The scan operation returns one or more items and item attributes by accessing every item in a table.

that’s it!

Conclusion

In this, we’ve learned to successfully create a DynamoDB NoSQL database on AWS as an Amazon DynamoDB table, add records using dynamic schema, and query the Amazon DynamoDB table.

Thank YOU!

Sign up for daily dose of tech articles at your inbox.
Loading