[Recipes] Retrieving Items from DynamoDB Tables - Get Item, Query & Scan

Problem: 

Retrieve Items from DynamoDB Tables - Get Item, Query & Scan.

Solution Summary: 

You can retrieve items through various ways including Get, Query and Scan APIs. 

Prerequisites: 

For this lab, you need to have completed previous labs in this section: http://cloudericks.com/en/recipe/creating-and-doing-basic-operations-dyn...

Solution Steps: 

Get Item

Gets an item based on the unique key (s) provided.

aws dynamodb get-item --table-name course_data --key {\"course_id\":{\"S\":\"c002\"},\"course_date\":{\"S\":\"2017/4/30\"}} --profile dynamodbdev

 

Note:

  1. Default is eventual consistency; can be changed by adding –consistent-read.

  2. Can’t be used against an index; local or global.

  3. All key attributes needs to be provided (as applicable); else will get ValidationException.

  4. You can get the consumed write capacity units for the command adding following option: --return-consumed-capacity TOTAL.

  5. For no items, returns nothing.

 

Query

aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id" --expression-attribute-values {\":id\":{\"S\":\"c002\"}} --profile dynamodbdev

aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id AND course_date=:dat" --expression-attribute-values {\":id\":{\"S\":\"c002\"},\":dat\":{\"S\":\"2017/4/30\"}} --profile dynamodbdev -- return-consumed-capacity TOTAL

aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id AND course_date BETWEEN :dat1 AND :dat2" --expression-attribute-values {\":id\":{\"S\":\"c002\"},\":dat1\":{\"S\":\"2017/4/30\"},\":dat2\":{\"S\":\"2017/4/30\"}} --profile dynamodbdev --return-consumed-capacity TOTAL

aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id AND course_date BETWEEN :dat1 AND :dat2" --filter-expression "duration_in_days BETWEEN :d1 AND :d2" --expression-attribute-values {\":id\":{\"S\":\"c002\"},\":dat1\":{\"S\":\"2017/4/30\"},\":dat2\":{\"S\":\"2017/4/30\"},\":d1\":{\"N\":\"30\"},\":d2\":{\"N\":\"60\"}} --profile dynamodbdev --return-consumed-capacity TOTAL

 

Sample Output:

{

    "Count": 1,

    "Items": [

        {

            "course_id": {

                "S": "c002"

            },

            "duration_in_days": {

                "N": "40"

            },

            "course_date": {

                "S": "2017/4/30"

            }

        }

    ],

    "ScannedCount": 1,

    "ConsumedCapacity": {

        "CapacityUnits": 0.5,

        "TableName": "course_data"

    }

}

 

Note:

  1. The count variable in output refers to actual items returned, scanned count refers to actual items read and billed. Items matching key and scanned and then filtered out of it.

  2. Input can be partition key, or partition key with sort key, or a range of sort keys.

  3. For no items, returns empty block.

  4. Default is eventual consistency; can be changed by adding –consistent-read.

  5. Can filter non key values; but any discarded values are still charged capacity wise.

  6. Can be used against an index; local or global.

  7. You can do a query from the console by going to the Items tab, selecting Query in the drop down and filling necessary fields.

 

Scan

aws dynamodb scan --table-name course_data --filter-expression "duration_in_days BETWEEN :d1 AND :d2" --expression-attribute-values {\":d1\":{\"N\":\"45\"},\":d2\":{\"N\":\"55\"}} --profile dynamodbdev --return-consumed-capacity TOTAL

 

Sample Output

{

    "Count": 1,

    "Items": [

        {

            "course_id": {

                "S": "c003"

            },

            "duration_in_days": {

                "N": "50"

            },

            "course_date": {

                "S": "2017/4/30"

            }

        }

    ],

    "ScannedCount": 3,

    "ConsumedCapacity": {

        "CapacityUnits": 0.5,

        "TableName": "course_data"

    }

}

 

Scan with NOT BETWEEN

aws dynamodb scan --table-name course_data --filter-expression "NOT duration_in_days BETWEEN :d1 AND :d2" --expression-attribute-values {\":d1\":{\"N\":\"45\"},\":d2\":{\"N\":\"55\"}} --profile dynamodbdev --return-consumed-capacity TOTAL

 

Scan with BETWEEN and NOT BETWEEN

aws dynamodb scan --table-name course_data --filter-expression "duration_in_days BETWEEN :d1 and :d2 OR NOT duration_in_days BETWEEN :d3 AND :d4" --expression-attribute-values {\":d1\":{\"N\":\"30\"},\":d2\":{\"N\":\"70\"},\":d3\":{\"N\":\"45\"},\":d4\":{\"N\":\"55\"}} --profile dynamodbdev --return-consumed-capacity TOTAL

 

Note:

  1. BETWEEN and NOT BETWEEN operators has higher precedence over AND/OR. Read more here.   

  2. Input is table name without keys.

  3. Filtering is possible, but still ALL DATA is read.

  4. The count variable in output refers to actual items returned, scanned count refers to actual items read and billed. Here, scanned count will be equal to number of items in table.

  5. Default is eventual consistency; can be changed by adding –consistent-read.

  6. Scans can be done in parallel after splitting them into segments, for improving performance.

  7. Query is always preferred over scan if possible.

  8. You can do a scan from the console by going to the Items tab, selecting Scan in the drop down and filling necessary fields.

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream