节点js CSV到JSON(大文件)

我需要在csv文件中将行转换为json。 我使用csvtojson软件包并且像魅力一样工作,但是我的csv文件非常庞大,大概有21000000行。 🙁

我的节点应用程序可以处理所有行。

看到我的snippet吼叫。

csv({noheader:true, delimiter:";", workerNum: 4}) .fromFile(csvFilePath) .on('json',(jsonObj, rowIndex)=>{ var TN = jsonObj.field1; var cod_op = jsonObj.field2; var msisdn = TN.toString(); //UP TO FIREBASE HERE!!! noticia.child(msisdn).set(cod_op); console.log(TN + ' ' + rowIndex); }) .on('done',(error)=>{ console.log() }) 

我用csv-stream模块自己解决了

这是一个snippset

  var options = { delimiter : ';', // default is , endLine : '\n', // default is \n, columns : ['VERSION_ID', 'TN','RN1','DATA','OPERACAO','CNL','EOT'], // by default read the first line and use values found as columns columnOffset : 7, // default is 0 escapeChar : '"', // default is an empty string enclosedChar : '"' // default is an empty string } var csvStream = csv.createStream(options); i = 0 request('http://yourhugefile.csv').pipe(csvStream) .on('error',function(err){ console.error(err); }) .on('data',function(data){ // outputs an object containing a set of key/value pair representing a line found in the csv file. //console.log(data); }) .on('column',function(key,value){ if(key == 'TN' || key == 'RN1') firebaseUpload(value); // outputs the column name associated with the value found //console.log('#' + key + ' = ' + value); })