Open In App

AWS DynamoDB – Working with Items & Attributes

Improve
Improve
Like Article
Like
Save
Share
Report

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 :
{
  "Color": true,
  "Director": "Christopher Nolan",
  "MovieID": 1,
  "Name": "Inception",
  "Rating": 8.7,
  "Year": 2010
}
Example 2:
{
  "Color": false,
  "MovieID": 99,
  "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:

Create Item

Tree Format

Tree Format

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

Batch Operations:

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.

Atomic Counter:

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.

Conditional Writes:

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. 


Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads