2012-11-16 64 views
3

我正在阅读使用JSONStream的大型JSON文件,并且我想在处理整个流时调用方法。使用事件流结束处理流

var JSONStream = require('JSONStream'), es = require('event-stream'); 

es.pipeline(
    fs.createReadStream('file.json'), 
    JSONStream.parse(['features', true]), 
    es.map(function (data) { 
    console.log("Added data"); 
    }) 
); 

我该怎么做?

回答

1

Sorted。将读取的流保存在一个变量中,并在该读取流上输入('end',function)。

2

我使用'event-stream'来处理〜200kB文件,里面包含JSONs,并在'end'时得到了问题当使用'event-stream'时,如果我把.on('end')从来没有调用过事件事件流管道之后。但是当我把它放在管道之前 - 一切正常!

stream.on('end',function() { 
      console.log("This is the End, my friend");     
    }).on('error',function (err) { 
       console.error(err); 
    }).pipe(es.split()) 
     .pipe(es.map(function (line, cb) { 
       //Do anything you want here with JSON of line 
       return cb(); 
      }));