-
-
Save EricCrosson/d84af8a083f79ddabcc6069c7994c57a to your computer and use it in GitHub Desktop.
Import CSV into 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
whateverId | attribute1 | someotherattribute | |
---|---|---|---|
foo | bar | baz | |
hello | erwin | world |
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
const fs = require('fs') | |
const parse = require('csv-parse/lib/sync') | |
const AWS = require('aws-sdk') | |
AWS.config.update({region: 'ap-southeast-2'}); | |
const docClient = new AWS.DynamoDB.DocumentClient() | |
const contents = fs.readFileSync('./<filename>.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 | |
docClient.put({TableName: '<Table>', Item: item}, (err, res) => { | |
if(err) console.log(err) | |
}) | |
}) |
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": "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