Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ChunAllen
Created April 29, 2022 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChunAllen/bc7e04d42956b93402066fb01f7e066e to your computer and use it in GitHub Desktop.
Save ChunAllen/bc7e04d42956b93402066fb01f7e066e to your computer and use it in GitHub Desktop.
Importing CSV to DynamoDB table
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}`)
})
})
{
"name": "dydbimport",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"aws-sdk": "^2.153.0",
"csv-parse": "^2.0.0"
}
}
@ChunAllen
Copy link
Author

Install Package using:

npm install

Run the app using

node index.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment