Skip to content

Instantly share code, notes, and snippets.

@maatthc
Created May 14, 2019 04:13
Show Gist options
  • Save maatthc/2958579474b3a396fe3eeeca27970f47 to your computer and use it in GitHub Desktop.
Save maatthc/2958579474b3a396fe3eeeca27970f47 to your computer and use it in GitHub Desktop.
Avoid the "JavaScript heap out of memory" : Example of how to process huge CSV files using Streams in Nodejs.
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const moment = require('moment')
const csv = require('fast-csv')
const highland = require('highland')
const writeableStream = fs.createWriteStream(path.join(__dirname, 'input.csv'))
const csvStream = csv.fromPath(path.join(__dirname, 'output.csv'), { headers: true, trim: true, objectMode: true })
const outputStream = csv.createWriteStream({ headers: true })
highland(csvStream).map(entry => {
const athenaDate = moment(entry.date, 'DD/MM/YY').format('YYYY-MM-DD')
return {
keycode: entry.keycode,
date: athenaDate,
qty: entry.qty
}
}).pipe(outputStream).pipe(writeableStream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment