Open In App

Read An Item From A DynamoDB Table

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Reading an item from a Dynamo table is a vital operation in application development that allows you to work on data efficiently. We will explore how we can read items from the table 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.

AWS DynamoDB Table Structure

  • 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 read this article.

Prerequisites

  • 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 with items in it, so you can read an item from it. If you don’t know how to create the table read this article and for writing items in it read this article.

Setting Up the DynamoDB Environment

There are 3 ways you can use DynamoDB:

  1. AWS Management Console – It is a web application that contains all services provided by Amazon and where you can manage all of your services. To know more about it read this article.
  2. CloudShell – It is a browser-based shell provided by Amazon itself on the AWS Management Console to quickly run scripts and perform certain tasks.
  3. AWS CLI – It is also a shell for local computers for advanced users and automating the scripts. To know more about it read this article.

Implementation:

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.

DynamoDB

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’).

Select Table Name

Step 2: From the top right press Explore table items. This will open the page to read items from the table using queries.

Table

Step 3: Select Query, write Partitionthe key, and Sort key to find in table. These keys are your primary keys which you set during the creation of the table. Hit Run.

6

Query

Step 4: The above output will return all the items with the Partition & Sort key that you put in.

Read an item using the CloudShell or Amazon CLI (Command Line Interface)

Step 1: After landing on the DynamoDB dashboard page select CloudShell from the bottom left. This will open the CLI to run queries on the table.

Cloudshell

Step 2: After Cloudshell has loaded, type the below command to read the item from the table.

aws dynamodb get-item --table-name Music --key '{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}}'
  • aws dynamodb -> Service name
  • get-item -> Operation to be performed
  • –table-name table_name -> Name of table, Music in my case.
  • –key ‘query’ -> Item you want to read from a given query.

Read item command

Step 3: Hit enter and all the items satisfying your query will be displayed.

Output: 

{
"Item": {
"AlbumTitle": {
"S": "Songs About Life"
},
"Awards": {
"S": "10"
},
"Artist": {
"S": "Acme Band"
},
"SongTitle": {
"S": "Happy Day"
}
}
}

CLI output

Conclusion

We have successfully read an item from the DynamoDB table (Music) using two methods Amazon Management Console and CloudShell (AWS CLI). Using GUI you can easily read items by just writing queries but if you want to read an item from outside Amazon Management Console we can use AWS CLI and convert output in JSON format.

Read an item from a DynamoDB table – FAQ’s

1. How to fetch all items from DynamoDB table?

To fetch all the items from dynamodb table you can use the following command in AWS CLI.

aws dynamodb scan --table-name your-table-name

2. How do I get data from DynamoDB?

  • get-item: This is used in AWS Command Line Interface (CLI). To retrieve an item you must specify a table name and keys that you want to retrieve.
  • Query: The items in the table can be retrieved by querying on the table. While querying, it by default contains the primary key as a search filter. More attributes can be added to refine search.
  • Scan: It is similar to the query. The only difference is that it doesn’t have any attribute by default for searching. To search an item, you must define an attribute, and its value to find the item.

To know more refer to AWS DynamoDB – Read Data from a Table



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads