Skip to content

Instantly share code, notes, and snippets.

@russomi
Forked from evanderkoogh/example.csv
Created December 3, 2021 23:48
Show Gist options
  • Save russomi/0b4e10dd2a1d8cb6f15befc823275d73 to your computer and use it in GitHub Desktop.
Save russomi/0b4e10dd2a1d8cb6f15befc823275d73 to your computer and use it in GitHub Desktop.
Import CSV into DynamoDB
whateverId attribute1 someotherattribute
foo bar baz
hello erwin world
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)
})
})
{
"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