DEV Community

Revathi Joshi for AWS Community Builders

Posted on

3

DynamoDB and its Data Pane Operations - 3

This is the 3rd and the last article in the series for Data Pane 1 (read-write) operations and Data Pane 2 (read-write) operations.

In this article, I am going to show you such operations on DynamoDB scan. Some of the criteria which can be used in the scan operations are - Filter Expression, with options, such as max items, --starting-token, Count and ScannedCount and Reserved words.

In this article, I am not explaining the terminology and the concepts used in DynamoDB Tables. Please visit my previous articles for this - DynamoDB and its Control Pane Operations - 1 and DynamoDB and its Control Pane Operations - 2.

Since the scan operation involves scanning the whole table and not just a single item, there is no Key condition expression.

Scan

  • Retrieves all items in the specified table or index.

  • You can retrieve entire items, or just a subset of their attributes.

Filter Expression

  • It removes some items from the Result Set returned by the Query.

  • This can be compared in SQL to this is the part of the WHERE clause that acts on the non-Key attributes.

Max items

  • If we want to limit the number of items then we use the --max-items option.

ScannedCount and Count

  • Counts the items in the results

  • If you don't use a filter expression, ScannedCount and Count have the same value.

Let’s get started!

Please visit my GitHub Repository for DynamoDB articles on various topics being updated on constant basis.

Pre-requisites:

  • AWS user account with admin access, not a root account.
  • Cloud9 IDE with AWS CLI.

Objectives:

1. Create a DynamoDB table

2. Add 3 items to the table

3. Scan the table to get a subset of the attributes

4. Scan the table to limit the items number to 1 using --max-items parameter.

5. Issue the scan request again, this time passing the NextToken value into the --starting-token parameter

6. Dealing with Reserved words

Resources Used:

Amazon DynamoDB Developer Guide

scan cli

Reserved words

Steps for implementation to this project:

  • Copy the and paste the code in your AWS Cloud9 command prompt.

1. Create a DynamoDB table

aws dynamodb create-table \
    --table-name Movies \
    --attribute-definitions \
        AttributeName=Title,AttributeType=S \
        AttributeName=Review,AttributeType=S \
    --key-schema \
        AttributeName=Title,KeyType=HASH \
        AttributeName=Review,KeyType=RANGE \
    --provisioned-throughput \
        ReadCapacityUnits=10,WriteCapacityUnits=5
aws dynamodb wait table-exists --table-name Movies
Enter fullscreen mode Exit fullscreen mode

Image description

2. Add 3 items to the table

aws dynamodb put-item \
    --table-name Movies \
    --item '{
           "Title": {"S": "Abhimaan"},
           "Review": {"S": "Excellent"},
           "Actor": {"S": "Big B"},
           "PostedBy": {"S": "User A"},
           "Songs": {"L": [ {"S": "Song1"},
                            {"S": "Song2"}
                          ]
                    }
            }'
aws dynamodb put-item \
    --table-name Movies \
    --item '{
           "Title": {"S": "Abhimaan"},
           "Review": {"S": "Terrific"},
           "Actor": {"S": "Jaya"},
           "PostedBy": {"S": "User B"},
           "Songs": {"L": [ {"S": "Song1"},
                            {"S": "Song2"}
                          ]
                    }
            }'
aws dynamodb put-item \
    --table-name Movies \
    --item '{
           "Title": {"S": "Abhimaan"},
           "Review": {"S": "Too Good"},
           "Actor": {"S": "Bindu"},
           "PostedBy": {"S": "User A"},
           "Songs": {"L": [ {"S": "Song1"},
                            {"S": "Song2"}
                          ]
                    }
            }'  
Enter fullscreen mode Exit fullscreen mode

Image description

3. Scan the table to get a subset of the attributes

  • Scan scanned all 3 items (ScannedCount) in the table -
    "ScannedCount": 3

  • But the Filter Expression reduced our result set size down to 2 items (Count) - "Count": 2

  • Hence, you should see 2 items in the result set, as the non-key attribute PostedBy references 2 items in Movies table.

  • --item '{
    "PostedBy": {"S": "User A"}

  • Refer the Movies table to check

aws dynamodb scan \
    --table-name Movies \
    --filter-expression 'PostedBy = :user' \
    --expression-attribute-values '{
        ":user" : {"S": "User A"}
    }' \
    --return-consumed-capacity TOTAL
Enter fullscreen mode Exit fullscreen mode

4. Scan the table to limit the items number to 1 using --max-items parameter.

aws dynamodb scan \
    --table-name Movies \
    --filter-expression 'PostedBy = :user' \
    --expression-attribute-values '{
        ":user" : {"S": "User A"}
    }' \
    --max-items 1 \
    --return-consumed-capacity TOTAL

Enter fullscreen mode Exit fullscreen mode
  • the scan response will show a NextToken which we can then issue to a next scan call to pick up the 2nd item.

NextToken



"NextToken": "eyJFeGNsdXNpdmVTdGFydEtleSI6IG51bGwsICJib3RvX3RydW5jYXRlX2Ftb3VudCI6IDF9"


Enter fullscreen mode Exit fullscreen mode

Image description

5. Issue the scan request again, this time passing the NextToken value into the --starting-token parameter

aws dynamodb scan \
    --table-name Movies \
    --filter-expression 'PostedBy = :user' \
    --expression-attribute-values '{
        ":user" : {"S": "User A"}
    }' \
    --max-items 1 \
    --starting-token eyJFeGNsdXNpdmVTdGFydEtleSI6IG51bGwsICJib3RvX3RydW5jYXRlX2Ftb3VudCI6IDF9 \
    --return-consumed-capacity TOTAL
Enter fullscreen mode Exit fullscreen mode

Image description

6. Dealing with Reserved words

  • In the Filter Expression, if the attribute name is actually a DynamoDB Reserved Word, it will throw an error - An error occurred (ValidationException) when calling the Scan operation: Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: Number

  • Because the Number attribute name is actually a DynamoDB Reserved Word.

  • This can be solved by putting a placeholder which is a # (pound sign) in the FilterExpression and provide the actual attribute name in the --expression-attribute-names

aws dynamodb scan \
    --table-name <Table Name> \
    --filter-expression 'Pictures >= :pictures AND #Number >= :comment' \
    --expression-attribute-values '{
        ":pictures" : {"N": "1"},
        ":Number" : {"S": "Nice"}
    }' \
    --expression-attribute-names '{"#Number" : "number"}' \
    --return-consumed-capacity TOTAL
Enter fullscreen mode Exit fullscreen mode

Cleanup

  • Delete the table Movies

What we have done so far

We have successfully demonstrated Data Pane operations on DynamoDB scan, using these criteria - Filter Expression, with options, such as max items, --starting-token, Count and ScannedCount, Reserved words.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post