Created
November 20, 2017 13:29
-
-
Save evanderkoogh/37f6adb01268e82ecd68d46fce1c7c5a to your computer and use it in GitHub Desktop.
Import CSV into DynamoDB
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
whateverId | attribute1 | someotherattribute | |
---|---|---|---|
foo | bar | baz | |
hello | erwin | world |
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') | |
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 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" | |
} | |
} |
BartusZak
commented
Sep 7, 2020
•
This should now be ...
const {parse} = require('csv-parse/sync');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment