Last active
September 3, 2023 03:55
-
-
Save midnightcodr/53872b4ed3571441009de6358e75d16e to your computer and use it in GitHub Desktop.
How to write two million rows of csv data using faker.js and node cluster
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
const faker = require('faker') | |
// const N = 30 | |
const N = 2000000 | |
const fs = require('fs') | |
const record = () => { | |
// faker.fake( | |
// '{{name.lastName}},{{name.lastName}},{{address.city}},{{address.county}},{{address.zipCode}},{{hacker.adjective}}\n' | |
return [ | |
faker.name.firstName(), | |
faker.name.lastName(), | |
faker.address.city(), | |
faker.address.state(), | |
faker.address.zipCode(), | |
faker.hacker.adjective() | |
] | |
} | |
const cluster = require('cluster') | |
const numCPUs = require('os').cpus().length | |
let workers = [] | |
let written = 0 | |
if (cluster.isMaster) { | |
masterProcess() | |
} else { | |
childProcess() | |
} | |
function masterProcess () { | |
console.log(`Master ${process.pid} is running`) | |
for (let i = 0; i < numCPUs; i++) { | |
console.log(`Forking process number ${i}...`) | |
const worker = cluster.fork() | |
worker.on('message', msg => { | |
if (msg === 'done') { | |
written += 1 | |
} | |
if (written % 100000 === 0) { | |
console.log(`written=${written}`) | |
} | |
if (written >= N) { | |
console.log('all done') | |
process.exit() | |
} | |
}) | |
workers.push(worker) | |
} | |
for (let i = 0; i < N; i++) { | |
const workerId = i % numCPUs | |
const worker = workers[workerId] | |
worker.send(i) | |
} | |
} | |
function childProcess () { | |
const writer = fs.createWriteStream('out.csv', { flags: 'a' }) | |
console.log(`Worker ${process.pid} started`) | |
process.on('message', message => { | |
const msg = record().join(',') + '\n' | |
writer.write(msg, () => { | |
// signal writing is done for each entry | |
process.send('done') | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instruction:
npm i faker
inside that foldertime node write-two-millio-rows-of-csv-data-with-node-cluster.js