Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.152.0

Reading and Writing Items in Batch in DynamoDB


                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to read and write batches of items in a DynamoDB table.

The Scenario

In this example, you use a series of Node.js modules to put a batch of items in a DynamoDB table as well as read a batch of items. The code uses the SDK for JavaScript to perform batch read and write operations using these methods of the DynamoDB client class:

Prerequisite Tasks

To set up and run this example, first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

Reading Items in Batch

Create a Node.js module with the file name ddb_batchgetitem.js. Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS.DynamoDB service object. Create a JSON object containing the parameters needed to get a batch of items, which in this example includes the name of one or more tables from which to read, the values of keys to read in each table, and the projection expression that specifies the attributes to return. Call the batchGetItem method of the DynamoDB service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { RequestItems: { 'TABLE_NAME': { Keys: [ {'KEY_NAME': {N: 'KEY_VALUE_1'}}, {'KEY_NAME': {N: 'KEY_VALUE_2'}}, {'KEY_NAME': {N: 'KEY_VALUE_3'}} ], ProjectionExpression: 'KEY_NAME, ATTRIBUTE' } } }; ddb.batchGetItem(params, function(err, data) { if (err) { console.log("Error", err); } else { data.Responses.TABLE_NAME.forEach(function(element, index, array) { console.log(element); }); } });

To run the example, type the following at the command line.

Copy
node ddb_batchgetitem.js

This sample code can be found here on GitHub.

Writing Items in Batch

Create a Node.js module with the file name ddb_batchwriteitem.js. Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS.DynamoDB service object. Create a JSON object containing the parameters needed to get a batch of items, which in this example includes the table into which you want to write items, the key(s) you want to write for each item, and the attributes along with their values. Call the batchWriteItem method of the DynamoDB service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { RequestItems: { "TABLE_NAME": [ { PutRequest: { Item: { "KEY": { "N": "KEY_VALUE" }, "ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }, "ATTRIBUTE_2": { "S": "ATTRIBUTE_2_VALUE" } } }, PutRequest: { Item: { "KEY": { "N": "KEY_VALUE" }, "ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }, "ATTRIBUTE_2": { "S": "ATTRIBUTE_2_VALUE" } } } } ] } }; ddb.batchWriteItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

To run the example, type the following at the command line.

Copy
node ddb_batchwriteitem.js

This sample code can be found here on GitHub.