如何在NodeJS中监听自定义可读流中的事件



这是我的代码:

class Counter extends Readable {
constructor() {
super();
}
_read () { 
readStream.on('data', (data) => {
console.log(JSON.stringify(data));
this.push(JSON.stringify(data));
});
readStream.on('end', () => {
this.push(null);
});
}
}

我得到了这个错误,处理也变得非常缓慢。

(node:18488) MaxListenersExceededWarning: Possible EventEmitter memory 
leak detected. 11 end listeners added. Use emitter.setMaxListeners() 
to increase limit (node:18488) MaxListenersExceededWarning: Possible 
EventEmitter memory leak detected. 11 data listeners added. Use 
emitter.setMaxListeners() to increase limit

这是我的完整代码。基本上,我想读CSV。将其转换为JSON并写入文件。我想使用流进行此操作。我正在使用"csv解析器"npm模块

const  fs = require('fs');
var Readable = require('stream').Readable; 
const csv = require('csv-parser');

class Counter extends Readable {
constructor() {
super();
}
_read () { 
readStream.on('data', (data) => {
console.log(JSON.stringify(data));
this.push(JSON.stringify(data));
});
readStream.on('end', () => {
this.push(null);
});
}
}
let writeStream = fs.createWriteStream('tmp1');
let readStream = fs.createReadStream('tmp.csv').pipe(csv());
var counter = new Counter(); 
counter.pipe(writeStream); 

我尝试将1.48GB的csv文件作为json数组写入该文件,它没有给出您提到的任何错误或警告。

const http = require('http');
const fs = require('fs');
const csv = require('csv-parser');
const server = http.createServer();
const writeStr = fs.createWriteStream('data.json', { flags: 'a' });
writeStr.write('[n');
let sep = "";
server.on('request', (req, res) => {
console.log(req.method);
fs.createReadStream('./Crimes_-_2001_to_present.csv').pipe(csv())
.on('data', function (data) {
writeStr.write(sep + JSON.stringify(data));
if (!sep)
sep = ",n";
})
.on('end', function () {
writeStr.write('n]')
console.log('DONE WRITING FILE');
res.end();
})
.on('error', function (err) {
consol.log(err);
})
})
server.listen(8000);

仅供参考:数据集取自https://catalog.data.gov/dataset?res_format=CSV

相关内容

  • 没有找到相关文章

最新更新