Open In App

How To Scan In DynamoDB ?

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

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. You can use Amazon DynamoDB to create a database table that can store and retrieve any amount of data, and serve any level of request traffic. Amazon DynamoDB automatically spreads the data and traffic for the table over a sufficient number of servers to handle the request capacity specified by the customer and the amount of data stored, while maintaining consistent and fast performance.

Please refer to this article before diving into Scan Operation in DynamoDB – DynamoDB Introduction

Scan Operation

Scan operation in Amazon DynamoDB reads every item in a table or a secondary index. By default, a Scan operation returns all of the data attributes for every item in the table or index.

Scan always returns a result set. If no matching items are found, the result set is empty. A single Scan request can retrieve a maximum of 1 MB of data.

To do the scan operation from the AWS Management Console, Follow the below steps:

  • Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  • In the navigation pane on the left side of the console, choose Tables.
  • Choose your table from the table list.
  • Choose Explore Table Items.
  • In the Scan or Query items, Choose Scan.
  • In the Select Attribute Projection list, Select whether you want all attributes to be returned or the specific attributes to be returned.
  • And Run

Let’s dive into the small example. I have the table named test-table which have the following items.

DynamoDB Table

Table items

If I want to do the scan operation to get all attributes from the table, Then

Scan Operation

Items returned after doing scan operation

But If you want specific attributes, then select them from specific projection. Here I am selecting only fname and lname, so only two attributes are returned.

Test Table example

The following code snippet in python will return all the Attributes.

Python3




import boto3
 
def lambda_handler(event, context):
    client = boto3.client("dynamodb")
     
    response = client.scan(
            TableName= "Your-Table-Name",
            Select='ALL_ATTRIBUTES' # This will select All Attributes and return        
        )
    return {
            "statusCode" : 200,
            "headers" : {},
            "body" : response
        }


The following code snippet will only return the specified attributes.

Python3




import json
import boto3
 
def lambda_handler(event, context):
    client = boto3.client("dynamodb")
     
    response = client.scan(
            TableName= "Your-Table-Name",
            AttributesToGet=[
                'fname','lname'
            ] # The specified attributes are returned from the table.     
        )
    return {
            "statusCode" : 200,
            "headers" : {},
            "body" : response
        }


This is how we can do Scan operation with Console and Code.

Scan Operation on DynamoDB – FAQ’s

Can I limit the number of items returned in a Scan operation?

Ans : Yes, you can use the Limit parameter to specify the maximum number of items to be returned by the Scan operation.

Does Scan operation read all the attributes of an item?

Ans : Scan reads all the attributes of an item. However, you can use the ProjectionExpression parameter to specify a subset of attributes to be returned.

When should I use Scan in DynamoDB?

Ans : Use Scan when you need to read all items in a table or when there is no specific query condition. It’s not recommended for large tables, as it can be resource-intensive.

What is the difference between Scan and Query in DynamoDB?

Ans : Scan reads the entire table and then filters the results, while Query retrieves items based on the key conditions. Query is more efficient when compared to Scan.



Like Article
Suggest improvement
Share your thoughts in the comments