Reputation: 14375
In my application i'm using node.js with amazon web service dynamoDB.I have already created one table and inserted item on that.Now i want to display item count of my table using describeTable.How can i do that.I tried below code but it's not working.
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: '****', secretAccessKey: '****',region: 'us-east-1'});
var svc = new AWS.DynamoDB();
svc.client.describeTable({TableName:"Users"}, function(err,result) {
if(!err){
console.log('result is '+result[ItemCount]);
console.log('success');
}
else{
console.log("err is "+err);
}
});
Upvotes: 1
Views: 9797
Reputation: 99
Remove the client after svc. Documentation
var svc = new AWS.DynamoDB();
svc.describeTable(params, function(err, data) {
if (err) {
// error
} else {
var table = data['Table'];
console.log(table['ItemCount']);
}
});
This has worked for me.
Upvotes: 3
Reputation: 31
The immediate answer to your question is that you'll need to use result.Table.ItemCount instead of result.ItemCount in your callback function. However be aware that this count isn't updated in real time and may not reflect recent inserts or deletes to the table. If you want the current item count, you'll need to scan the table and use the Count property to get a count of items scanned. Scanning like this could consume all of your provisioned capacity for the table, so if this is a requirement be sure to balance the need for the most recent item count with other operations that might be running on the table at the same time.
Here's node.js sample of a scan that returns the item count. Since scan is called iteratively until all rows are read, I'm using the async module to wait for the results before issuing the next loop.
var async = require('async');
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'AKID',
secretAccessKey: 'secret',
region: 'us-east-1'});
var svc = new AWS.DynamoDB();
var scanComplete = false,
itemCountTotal = 0,
consumedCapacityUnitsTotal = 0;
var scanParams = { TableName : 'usertable',
Count : 'true' };
// scan is called iteratively until all rows have been scanned
// this uses the asyc module to wait for each call to complete
// before issuing the next.
async.until( function() { return scanComplete; },
function (callback) {
svc.scan(scanParams, function (err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
if (typeof (result.LastEvaluatedKey) === 'undefined' ) {
scanComplete = true;
} else {
// set the start key for the next scan to our last key
scanParams.ExclusiveStartKey = result.LastEvaluatedKey;
}
itemCountTotal += result.Count;
consumedCapacityUnitsTotal += result.ConsumedCapacityUnits;
if (!scanComplete) {
console.log("cumulative itemCount " + itemCountTotal);
console.log("cumulative capacity units " + consumedCapacityUnitsTotal);
}
}
callback(err);
});
},
// this runs when the loop is complete or returns an error
function (err) {
if (err) {
console.log('error in processing scan ');
console.log(err);
} else {
console.log('scan complete')
console.log('Total items: ' + itemCountTotal);
console.log('Total capacity units consumed: ' + consumedCapacityUnitsTotal);
}
}
);
Upvotes: 3
Reputation: 1919
ToddB,
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#scan-property describes scan's param having a Select parameter that can have value 'COUNT' ...
Select — (String) The attributes to be returned in the result. You can retrieve all item attributes, specific item attributes, the count of matching items, or in the case of an index, some or all of the attributes projected into the index.
COUNT: Returns the number of matching items, rather than the matching items themselves.
So it seems like in your example, scanParams should instead be ...
var scanParams = {
TableName: 'usertable',
Select: 'COUNT'
};
Upvotes: 2