Created
November 27, 2019 20:40
-
-
Save jesusgoku/5c471595b2de9ffafc85bc30c46df7c2 to your computer and use it in GitHub Desktop.
Process and generate Big files with NodeJS
This file contains hidden or 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 path = require('path'); | |
const readStream = fs.createReadStream(path.join(__dirname, 'generated-big-file.csv')); | |
const writeStream = fs.createWriteStream(path.join(__dirname, 'generated-big-file-copy.csv')); | |
readStream.pipe(writeStream); | |
readStream.on('end', () => writeStream.end()); |
This file contains hidden or 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 readline = require('readline'); | |
const { stdin, stdout } = process; | |
const rl = readline.createInterface({ | |
input: stdin, | |
output: stdout, | |
terminal: false, | |
}); | |
rl.on('line', line => console.log(line)); |
This file contains hidden or 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 { stdin, stdout } = process; | |
stdin.pipe(stdout); |
This file contains hidden or 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 path = require('path'); | |
const REGISTERS = 100000000; | |
const CHUNK_SIZE = 100000; | |
const stream = fs.createWriteStream(path.join(__dirname, 'generated-big-file.csv')); | |
let i = 0; | |
const write = () => { | |
if (i >= REGISTERS) { | |
stream.end(); | |
return; | |
} | |
let chunk = ''; | |
for (let j = i; j < (i + CHUNK_SIZE); j++) { | |
chunk += `${j}, Jesús, Urrutia, [email protected], male, 1985-12-18\n`; | |
} | |
stream.write(chunk, write); | |
i += CHUNK_SIZE; | |
} | |
write(); |
This file contains hidden or 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
let i = 0; | |
const next = () => process.stdout.write(`${i++}\n`, next); | |
next(); |
This file contains hidden or 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 process = require('process'); | |
const { Transform } = require('stream'); | |
const UpperCaseTransform = new Transform({ | |
transform(chunk, encoding, callback) { | |
this.push(chunk.toString().toUpperCase()); | |
callback(); | |
} | |
}); | |
process.stdin.pipe(UpperCaseTransform).pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment