2017-02-16 77 views
4

我需要将csv文件中的行转换为json。我使用csvtojson软件包并且像魅力一样工作,但是我的csv文件非常庞大,大约有21000000行。 :(节点js CSV到JSON(大文件)

我节点应用确实可以处理所有行。

见我的片段波纹管。

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() 
    }) 

回答

2

我由我自己解决了csv-stream module

这里是一个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); 
    })