DynamoDB – Creating Items
Last Updated :
13 Feb, 2024
Creating an item in a DynamoDB table is a vital operation in application development that allows you to upload data. We will explore how we can create items for tables using different methods used for different purposes.
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. With DynamoDB, you can create database tables that can store and retrieve any amount of data and serve any level of request traffic. You can scale up or scale down your table throughput capacity without downtime or performance degradation. To know more about it read this article.
Components Of DynamoDB
- AWS Account: Before starting this tutorial, you must have an AWS account. Read this article in case you don’t have an AWS account.
- DynamoDB Table: You must have a table, so you can create/write an item into it. If you don’t know how to create the table read this article.
Data Model in DynamoDB
- Tables: Similar to any other database system, DynamoDB stores data in tables. A table is a collection of items.
- Items: An item is a group of attributes that is uniquely identifiable among all of the other items. Each table contains zero or more items.
- Attributes: Each item is composed of one or more attributes. An attribute is a fundamental data element, something that does not need to be broken down any further.
For example, consider table ‘People’.It has 3 items in it. Each item has 3 attributes namely, FirstName, LastName, and Age. For a more detailed overview of the table read this article.
Primary Key: The primary key uniquely identifies each item in the table, so that no two or more items can have the same key.
Types Of Keys in DynamoDB
- Partition key – A simple primary key, composed of one attribute known as the partition key. It uses the partition key’s value as input to an internal hash function.
- Partition key and Sort key – It is a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.
For a more detailed overview about key read this article.
Step-By-Step Guide To Create Items In AWS
Step 1: Login into AWS -> https://aws.amazon.com/ Management
Step 2: After signing in you will land on the AWS Management Console page and search for Dynamo as shown below.
Read an item using the Amazon Management Console (GUI)
Step 1: After landing on the DynamboDB dashboard, go to Tables from the sidebar and click on your table name (in my case ‘Music’).
Step 2: From the Top right go to Explore table items. This will allow you to perform CRUD operations on table.
Step 3: Select Create item, from this you will be able to create an item for the table.
Step 4: Add data in your respective fields. We can also add additional attributes in table by clicking on Add new attribute and then selecting datatype of your data.
Step 5: After filling all the fields Create item.
Step 6: After refreshing exploring items page you can see you create item at the bottom.
Read an item using the Amazon CLI (Command Line Interface)
Step 1: Select Cloudshell from the bottom left or just open AWS CLI on your system.
Syntax:
aws dynamodb put-item --table-name TABLE-NAME --item '{"ATTRIBUTE" : {"DATATYPE":"DATA"}}'
TABLE-NAME: add your table name
ATTRIBUTE: add your attribute name
DATATYPE: In CLI there are different datatype descriptors for each datatype.
- S – String
- N – Number
- B – Binary
- BOOL – Boolean
- NULL – Null
- M – Map
- L – List
- SS – String Set
- NS – Number Set
- BS – Binary Set
For Example:
aws dynamodb put-item --table-name Music --item '{"Artist" : {"S":"Bring be the horizon"}, "Title" : {"S":"Strangers"}, "Year" : {"N":"2022"}}'
Note: You can add any number of attributes for each item.
Step 2: Check if successfully created or not. Goto explore items page and check at the bottom.
Best Practices for Creating Items
- Primary Key Design: Select Partion key or Composite according to your data you are going to insert. In above example we selected composite key because Artists name can be repeated but both artist name and song can’t be similar.
- Large Items and Attributes: DynamoDB currently limits the size of each item (400 KB) that is stored in a table, which includes both attribute names and values binary length.
- Timestamps: While using timestamp as one of your attributes it is good to use ISO time format (YYYY-MM-DD’T’hh:mm:ss’Z’).
Conclusion
We have successfully created an item into the DynamoDB table (Music) using two methods Amazon Management Console and CloudShell (AWS CLI). Using GUI you can easily create items by just filling fields but if you want to create an item from outside Amazon Management Console we can use AWS CLI.
Creating Items In DynamoDB – FAQ’s
1. What are items in DynamoDB?
An item is the basic data unit in Amazon DynamoDB. It represents a series of attributes, each with a name and a value, and is practically comparable to a row in a relational database table.
2. What is the maximum size of an item in DynamoDB?
The maximum size of an item in the DynamoDB IS 400 KB. Although 400KB is a considerable amount less than the other alternatives, it is still large enough for the majority of typical database activities.
3. Can we increase item size in DynamoDB?
No an item which is storing in DynamoDB can’t be excesses 400 kb. One MB of scanned data is the maximum for query and scan activities.
Share your thoughts in the comments
Please Login to comment...