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.
Step 2
Click on Tables on the left then click on create table on the right.
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.
7) Accept the default settings.
8) Click Create table.
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.
4) Under Attributes, for the userId Attribute name, type:
12345-abcd-6789
5) For the lastDateWatched attribute, type:
1619156406
- This is a UNIX timestamp.
6) Click Add new attribute to expand the dropdown list.
7) Choose String.
8) For the Attribute name, type:
videoId
9) For the value, type:
9875-djac-1859
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
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
21) Click Insert a field to expand the dropdown list again to add another field to the list.
22) Choose String.
23) In the value field, type:
Amazon Fire Tablet
24) Click Create item.
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.
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.
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.
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.
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.
- All items in your Amazon DynamoDB table should be displayed.
Congratulations, you have successfully created, queried and scanned your first NoSQL Database.
Top comments (0)