AWS DynamoDB is a NoSQL managed database that stores semi-structured data i.e. key-value and document data. It stores data in form of an item. An item consists of attributes. Upon table creation in DynamoDB, it only requires a primary key to differentiate between items and no schema is to be defined. Each item can have a different number of attributes.
Example 1 :
"Director": "Christopher Nolan",
"Name": "Schindler's List"
In the above examples, two different items have been shown. We observe that each item contains a different number of attributes. One attribute MovieID should always be present in each item and its value cannot be the same in two items as it is the primary key.
Create a Table:
To create items, first, we must create a table in DynamoDB with a partition key. A table named Movies has already been created with MovieID as our partition key. See the below image:
Add items to the table:
Navigate to the Items tab of the table, and click Create items to add items to the table. Items can be added via Tree format or via text. Both of them have been shown below:
In the above items, one attribute MovieID will always be present. Rest of the attributes, either you can add or delete from the item.
Writing an Item to the Table:
To write an item to a table, there are two ways. Either we can add items to the table via Amazon CLI (Command Line Interface) or via the Amazon Management Console. To write items, first create a table and once it is created, simply navigate to the Items tab of the table and click Create Item. Specify the attributes and their value and then click Save. This will write items to the table.
For more information visit.
Reading Item from a Table:
Reading an item from a table can be done using three ways. These are get-item, Query, and Scan. Query and scan methods are used for Amazon Management Console and the get-item method is used when we are trying to read data from a table using Amazon CLI (Command Line Interface).
For more information refer to this.
In batch operations, you either write or read multiple items from the Dynamodb table using PartiQL. The two operations are given below :
- BatchGetItem: To read multiple items. It can read about 100 items from one or more tables.
- BatchWriteItem: To create or delete items. It can either write or delete up to 25 items from one or more tables.
To implement an atomic counter, use the UpdateItem operation. The atomic counter is a numeric attribute that is incremented, unconditionally, without interfering with other write requests i.e. all write requests are implemented in the order in which they were received. The numeric value increments each time you call UpdateItem. For instance, we can use the atomic counter to track the visitors to a website. Each time a user hits our website, the counter gets incremented.
By default, the DynamoDB overwrites the existing item that has specified primary key on PutItem, UpdateItem, and DeleteItem operation requests. To solve this problem, DynamoDB provides a feature of conditional writes. A conditional write will only succeed if some criteria are met. For example, the PutItem request will only succeed if there is no item present with the same primary key or could prevent from overwriting an existing item if requirements are met.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!