Created
August 27, 2017 22:42
-
-
Save viatsko/c939c505a9242313d0278829d1cee88c to your computer and use it in GitHub Desktop.
Node.JS - reading log line-by-line
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
// https://stackoverflow.com/a/23695940/844204 | |
var fs = require('fs') | |
, util = require('util') | |
, stream = require('stream') | |
, es = require('event-stream'); | |
var lineNr = 0; | |
var s = fs.createReadStream('very-large-file.csv') | |
.pipe(es.split()) | |
.pipe(es.mapSync(function(line){ | |
// pause the readstream | |
s.pause(); | |
lineNr += 1; | |
// process line here and call s.resume() when rdy | |
// function below was for logging memory usage | |
logMemoryUsage(lineNr); | |
// resume the readstream, possibly from a callback | |
s.resume(); | |
}) | |
.on('error', function(err){ | |
console.log('Error while reading file.', err); | |
}) | |
.on('end', function(){ | |
console.log('Read entire file.') | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment