Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch/scan all items from `AWS dynamodb` using node.js

How to fetch/scan all items from AWS dynamodb using node.js. I am posting my code here.

var docClient = new aws.DynamoDB.DocumentClient();     var params = {     TableName:"users",     KeyConditionExpression:"user_status=:status",     ExpressionAttributeValues: {         ":status": "Y"     }     };      var queryExecute = function(callback) {         docClient.query(params,function(err,result) {             if(err) {                 console.log(err)                 callback(err);                 } else {                 console.log(result);                  if(result.LastEvaluatedKey) {                     params.ExclusiveStartKey = result.LastEvaluatedKey;                     queryExecute(callback);                     } else {                         callback(err,items);                     }                 }             });         }         queryExecute(callback);  

This is giving me below error.

ValidationException: Query condition missed key schema element: `user_id`. 

Here primary key is user_id. I don't want to use it with my query condition, because I need to set a value if I mentioned primary key in KeyConditionExpression. May be I am wrong. However please suggest me a good way to fetch all items from dynamodb, which is having user_status = "Y"

like image 501
Vishnu T S Avatar asked Jun 16 '17 13:06

Vishnu T S


People also ask

Does DynamoDB Scan return all items?

A 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. You can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them.

How do I get items from DynamoDB?

To read an item from a DynamoDB table, use the GetItem operation. You must provide the name of the table, along with the primary key of the item you want. The following AWS CLI example shows how to read an item from the ProductCatalog table. With GetItem , you must specify the entire primary key, not just part of it.


2 Answers

This is working for me:

export const scanTable = async (tableName) => {     const params = {         TableName: tableName,     };      const scanResults = [];     const items;     do{         items =  await documentClient.scan(params).promise();         items.Items.forEach((item) => scanResults.push(item));         params.ExclusiveStartKey  = items.LastEvaluatedKey;     }while(typeof items.LastEvaluatedKey !== "undefined");          return scanResults;  }; 
like image 149
Hank Avatar answered Sep 21 '22 15:09

Hank


If you would like to get the data from DynamoDB without using Hash key value, you need to use Scan API.

Note: The Scan API reads all the items in the table to get the results. So, it is a costly operation in DynamoDB.

Alternate Approach : Use GSI

Scan Code for the above sceanario:-

var docClient = new AWS.DynamoDB.DocumentClient();  var params = {     TableName: "users",     FilterExpression: "#user_status = :user_status_val",     ExpressionAttributeNames: {         "#user_status": "user_status",     },     ExpressionAttributeValues: { ":user_status_val": 'somestatus' }  };  docClient.scan(params, onScan); var count = 0;  function onScan(err, data) {     if (err) {         console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));     } else {                 console.log("Scan succeeded.");         data.Items.forEach(function(itemdata) {            console.log("Item :", ++count,JSON.stringify(itemdata));         });          // continue scanning if we have more items         if (typeof data.LastEvaluatedKey != "undefined") {             console.log("Scanning for more...");             params.ExclusiveStartKey = data.LastEvaluatedKey;             docClient.scan(params, onScan);         }     } } 
like image 34
notionquest Avatar answered Sep 22 '22 15:09

notionquest