×


How to read data from AWS DynamoDB tables using AWS Lambda. get, scan and query methods(Node.js).

This tutorial aims at explaining how to read data from the AWS DynamoDB table.

AWS SDK has a class AWS.DynamoDB.DocumentClient, which has methods scan, get and query, that are used to read data from the AWS DynamoDB table.  Documentation on various methods provided by the class is available from the following link AWS.DynamoDB.DocumentClient – Documentation.

 

Table structure

primary partition key – name

sort key – age

table name – testTable

This may not be the ideal table structure but here we use it just as an example.

 

 

step 1: Executing the query to get all data from a table – scan method.

The scan method is used to  read every item in the table and returns all the data in the table. Filters can be also applied

First of all, we create an object scanningPrams which has all the params including the table name, required to scan the DynamoDB table.

Simple params to get 10 items from a table “testTable“, would look like this.

The output will have the following structure

The output will have three properties:  Items will have the queried items, ScannedCount will have the number of items evaluated before any scanFilter is applied, Count will have the number of items in the response.

Scanning means all the items in the table are looped through and it is very expensive. For querying a single item, we should use the get method with the primary key (Partition key and Sort key).

 

 

 

step 2: Querying a specific item using the primary key- get method

 get method is used to read the item from a table using primary key

Imagine we have an item in the table with the primary partition key  “name” and primary sort key “age”. The following code can be used to query for items with a specific name and age. get method is used to query items based on the primary key. Remember in cases with a partition key and sort key, they both together give the primary key.

This query will return a specific item from the table

The partition key is used for partitioning the data. Data with the same partition key are stored together, which allows you to query the data with the same partition key in a single query. This we will see in the next step.

The optional sort key determines the order of how data with the same partition key are stored

 

 

 

step 3: Query only with the partition key. Items with the same partition key are returned – query method.

The query method is used to retrieve data from a table. You must specify a partition key value; the sort key is optional.

For querying only using the primary key ignoring the partition key, the query method is used. Our table has a primary key as “name”, so we will query by name. Three properties are required for using the query method.

KeyConditionExpression – Will have the condition to be evaluated while querying.

ExpressionAttributeNames – Will have the attribute names. In our case, it is “name”.

ExpressionAttributeValues – Will have the attribute values. In our case, it is the value for “name”.

The code will look like this.

This will return all items with the same partition key.

 

So we have seen how to work with scan, get and query. Here is a dummy code with Amazon API Gateway integrated for you to test it out.

Thanks for reading. Don’t hesitate to comment if there are any errors or suggetions that you want to add to this article.

 

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *