Creating a NoSQL Database on AWS
Tags: nosql, dynamodb, amazon, aws
Published: 2022-09-13 16:53:48

Today we will be using the DynamoDB service on AWS to create a NoSQL database on Amazon.

NoSQL databases (also known as "not only SQL") are non-tabular databases and store data differently than relational tables. Depending on the data model, there are many types of NoSQL databases. Document, key-value, wide-column, and graph are the primary types. They offer flexible schemas and scale well under heavy user loads and data loads.

### Scenario:

A streaming company wants to track customers' movie streaming and so we will create a database to help track their customers' behaviors from metadata such as movies watched and device types.  

### Solution:
This solution uses Amazon DynamoDB which has a flexible schema - meaning each row can have any number of attributes at any point in time. In this lab we will: Create a NoSQL database as an
Amazon DynamoDB table, add records using dynamic schema to
Amazon DynamoDB and query an Amazon DynamoDB table.

Amazon DynamoDB is serverless with no servers to provision, patch, or manage and no software to install, maintain, or operate. Amazon
DynamoDB automatically scales tables up and down to adjust for capacity and maintain performance.

### Tasks to achieve our goal

- Create an Amazon DynamoDB table.
- Enter a new record and search for the record by partition ID.
- Create a new user item with a unique id.
- Add a new attribute named rating (Number).

## Step 1
Log into your AWS console, click services on the top left hand side of your management console, navigate to database and select DynamoDB.

![AWS Management Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v81cshg6qcfv9dv0aonh.png)

## Step 2
Click on Tables on the left then click on create table on the right.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rg6hp9qx5swmmqw9315x.png) 

Amazon DynamoDB supports both Key-value and document data models. This enables Amazon DynamoDB to have a flexible schema, so each row can have any number of columns at any point in time. This allows you to easily adapt the tables as your business requirements change, without having to redefine the table schema as you would in relational databases.

## Step 3
1) Under Table name type: 

> 
**UserVideoHistory**


2) Under Partition key type: 

> 
**userId**

Note: You must type the partition key exactly as shown. It is userId with an uppercase i.

3) Choose String data type from the dropdown list.

4) Under Sort key type: 

> 
**lastDateWatched**

5) Choose Number data type from the dropdown list.

6) Scroll down to the end of the page.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ej5ms5ywjt5l0muboa7.png) 

7) Accept the default settings. 

8) Click Create table.


![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18vmfzi914a61owt42j4.png)


When you create a table, in addition to the table name, you must specify the primary key of the table. The primary key uniquely identifies each item in the table, so that no two items can have the same key.

9) Review the Status of the table.

10) Once the status changes to Active, click the table name.

## Step 4
1) Under Tables, click the radio button to select the created table.

2) Click Actions to expand the dropdown list.

3) Choose Create item.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/843j5p1n3vy0xou8yk8r.png)

4) Under Attributes, for the userId Attribute name, type: 

> 
**12345-abcd-6789**


5) For the lastDateWatched attribute, type: 

> 
**1619156406** 

- This is a UNIX timestamp.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t85aurlje8f9pkpqmlh3.png)

6) Click Add new attribute to expand the dropdown list.  

7) Choose String.


![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6wktx3bmjob48065xrk.png)

8) For the Attribute name, type: 

> 
**videoId**

9) For the value, type: 

> 
**9875-djac-1859**

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw4vdi547pb5pfuu6bt7.png)

10) To add another attribute, click the Add new attribute dropdown list.

11) Choose String.

12) For the Attribute name, type: 

> 
**preferredLanguage**

13) For the value, type: 

> 
**English**

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztfolqpau0qfwx4r03c5.png)

14) Click Add new attribute to expand the dropdown list again to add another attribute.

15) Choose List.

16) For the Attribute name, type: 

> 
**supportedDeviceTypes**

17)  Click Insert a field to expand the dropdown list.

18) Choose String.

19) Click + beside the supportDeviceTypes Attribute.

20) In the value field, type: 

> 
**Amazon Fire TV**

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yce6wrh0d1gf1ks2i4uf.png)

21) Click Insert a field to expand the dropdown list again to add another field to the list.

22) Choose String.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wesbqj8mgegh1vlfzs5.png)

23) In the value field, type: 

> 
**Amazon Fire Tablet**

24) Click Create item.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vd9rrqt60463cy660amj.png)

After you've created a record you can still edit it. That includes the contents of the record and its attributes.

## Step 5
1) On the left menu, under Tables, click Explore items.

2) Click the userId 1234-abcd-6789 to edit the record again.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kkpd7yy6ucrzdrb6sng.png)

3) Click Add new attribute to expand the dropdown list to add another attribute.

4) Choose Number.

5) In the attribute name field, type: 

> 
**lastStopTime**

6) In the value field, type: 

> 
**90**

7) Click Save changes.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7swnnibc2irt2ysgx2h.png)

## QUERYING
The Query operation in Amazon DynamoDB finds items based on
primary key values. You must provide the name of the partition key attribute and a single value for that attribute.
The query returns all items with that partition key value. Optionally, you can provide a sort key attribute and use a
comparison operator to refine the search results.

1) Click to expand Scan/Query items.

2) Click the Query tab.

3) Under userId (Partition key), type: 

> 
**12345-abcd-6789**

4) Under lastDateWatch (Sort Key), choose Greater than.

5) In the value field type: 

> 
**1609477200**
 
6) Click Run.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2x0nejue3qfuqkfon0t.png)

## Next step
When running a Query operation the table looks for an exact match for the partition key and uses the sort key (if provided) as way to further limit the results.

1) Under Items returned, review the record returned.

2) To change the query criteria in the userId field, type: 

> 
**abd5-zxcg-12385**

3) Click Run.

4) Review the results. 

- Nothing is returned since there is no record that matches the partition key and has a sort key greater than what has been entered.

![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9avngksjse1bewltvga.png)

## SCANNING 

The Scan operation returns one or more items and item attributes by accessing every item in a table or a secondary index. If the total number of scanned items exceeds the maximum dataset
size limit of 1 MB, the scan stops and results are returned to the user as a LastEvaluatedKey value, to continue the scan in a subsequent operation. 

The results also include the number of items exceeding the limit. A scan can result in no table data meeting the filter criteria.

1) Click the Scan tab.

2) Click Run.

3) Review the results. 


![AWS DynamoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pwl6h1b85bsq72jg3ld.png)

- All items in your Amazon DynamoDB table should be displayed.


Congratulations, you have successfully created, queried and scanned your first NoSQL Database.