Created
April 29, 2022 04:31
-
-
Save ChunAllen/bc7e04d42956b93402066fb01f7e066e to your computer and use it in GitHub Desktop.
Importing CSV to DynamoDB table
This file contains 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
const fs = require('fs') | |
const parse = require('csv-parse/lib/sync') | |
const AWS = require('aws-sdk') | |
var credentials = new AWS.SharedIniFileCredentials({ profile: '<aws-profile>' }); | |
AWS.config.update({ | |
region: 'ap-southeast-1', | |
credentials: credentials | |
}); | |
const docClient = new AWS.DynamoDB.DocumentClient() | |
const contents = fs.readFileSync('./selected.csv', 'utf-8') | |
// If you made an export of a DynamoDB table you need to remove (S) etc from header | |
const data = parse(contents, { columns: true }) | |
data.forEach((item) => { | |
if (!item.maybeempty) delete item.maybeempty // need to remove empty items | |
item.version = parseInt(1) // added this since my table has sort key | |
docClient.put({ | |
TableName: '<table-name>', | |
Item: item | |
}, (err, res) => { | |
if (err) console.log(err) | |
console.log(`Inserting ${item.name}`) | |
}) | |
}) |
This file contains 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": "dydbimport", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"aws-sdk": "^2.153.0", | |
"csv-parse": "^2.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install Package using:
Run the app using