简体   繁体   中英

Function to scan AWS Dynamo DB recursively for Nodejs

So I need a recursive function in node.js for replacing this function call:

docClient.scan(params, callback)

More info see http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.04.html

Here is the recursive code to execute the scan until LastEvaluatedKey is available.

var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');

AWS.config.update({
    region: "us-west-2",
    endpoint: "http://localhost:8000",
    credentials : creds
});

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
    TableName: "Movies"
};

console.log("Scanning Movies table.");
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 {
        // print all the movies
        console.log("Scan succeeded.");
        data.Items.forEach(function(movie) {
           console.log("Item :", ++count,JSON.stringify(movie));
        });

        // continue scanning if we have more movies
        if (typeof data.LastEvaluatedKey != "undefined") {
            console.log("Scanning for more...");
            params.ExclusiveStartKey = data.LastEvaluatedKey;
            docClient.scan(params, onScan);
        }
    }
}  

Recursive DynamoDB Scan using AWS SDK V3.

import {
  DynamoDBClient,
  ScanCommand,
  ScanCommandOutput,
  ScanCommandInput,
} from '@aws-sdk/client-dynamodb';
import { unmarshall, marshall } from '@aws-sdk/util-dynamodb';

const client = new DynamoDBClient({ region: 'eu-west-1' });

export const scanTable = async<ResultType>(params: ScanCommandInput): Promise<ResultType[]> => {
  const scanParams: ScanCommandInput = params;
  const results = [];
  let lastEvaluatedKey: string;
  do {
    console.log(`Running query: ${JSON.stringify(scanParams)}`);
    const { Items, LastEvaluatedKey } = await client.send(new ScanCommand(scanParams));
    lastEvaluatedKey = LastEvaluatedKey;
    Items.forEach((item) => results.push(unmarshall(item)));
    scanParams.ExclusiveStartKey = LastEvaluatedKey;
  } while (typeof lastEvaluatedKey !== 'undefined');

  return results as ResultType[];
};

const items = await scanTable({ TableName: 'dynamo-table' })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM