Last active
March 13, 2020 13:29
-
-
Save furudean/2cda5dde81431f73d758960eaefead6a to your computer and use it in GitHub Desktop.
Clone a table with a small data sets from DynamoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { DocumentClient, } from "aws-sdk/clients/dynamodb"; | |
| import { chunk } from "lodash"; | |
| const dynamoDb = new DocumentClient(); | |
| const sourceTable = ''; | |
| const destinationTable = ''; | |
| const chunkSize = 25; | |
| const sleepTime = 1000; | |
| async function sleep(ms: number): Promise<void> { | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| function toPutRequest(item: DocumentClient.AttributeMap) { | |
| return dynamoDb.put({ | |
| Item: item, | |
| TableName: destinationTable | |
| }).promise() | |
| } | |
| async function runner() { | |
| try { | |
| console.log(`getting data from ${sourceTable}`); | |
| const dataFromSourceTable = await dynamoDb.scan({ | |
| TableName: sourceTable, | |
| }).promise() | |
| for (const items of chunk(dataFromSourceTable.Items, chunkSize)) { | |
| const startTime = new Date().getTime(); | |
| const requests = items.map(toPutRequest); | |
| await Promise.all(requests); | |
| const elapsed = new Date().getTime() - startTime; | |
| console.log(`put ${items.length} to ${destinationTable} in ${elapsed} ms`) | |
| await sleep(sleepTime) | |
| } | |
| console.log('done!'); | |
| } catch (error) { | |
| console.error(error) | |
| } | |
| } | |
| runner(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "dynamodb-copy", | |
| "version": "1.0.0", | |
| "description": "", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1", | |
| "start": "ts-node index.ts" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "aws-sdk": "*", | |
| "lodash": "^4.17.15" | |
| }, | |
| "devDependencies": { | |
| "@types/lodash": "^4.14.149", | |
| "@types/node": "^13.9.1", | |
| "ts-node": "^8.6.2", | |
| "typescript": "^3.8.3" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
| { | |
| "compilerOptions": { | |
| "target": "ESNext", | |
| "module": "commonjs", | |
| "esModuleInterop": true, | |
| "forceConsistentCasingInFileNames": true, | |
| "typeRoots": [ | |
| "node_modules/@types" | |
| ] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment