Node.js 流写入 MongoDB - 关注性能

Node.js stream writing to MongoDB - concerned about performance

我需要读取包含数千行的日志文件并将每一行写入 Mongo 数据库。我正在使用节点流读取文件。我正在使用 'split' npm 包将文件拆分为 'lines'。由于网络方面的考虑,MongoDB 写入将比日志文件读取花费更长的时间。

我的核心代码如下所示:

var readableStream = fs.createReadStream(filename);

            readableStream
                .pipe(split()) // This splits the data into 'lines'
                .on('data', function (chunk) {

                    chunkCount++;
                    slowAsyncFunctionToWriteLogEntryToDatabase(chunk); // This will take ages

                })
                .on('end', function () {
                    // resolve the promise which bounds this process
                    defer.resolve({v:3,chunkCount: chunkCount})

                });

我是否需要担心 MongoDB 系统会因排队的写入数量而受到重创?大概节点管道背压机制不知道大量的数据库写入正在排队?有什么方法可以 'slow' 可读流,以便它在从日志文件读取下一行之前等待每个 MongoDB 插入完成?我是不是多虑了?

您可以在可读流中使用 pause method 在将块写入 mongodb 时停止流。

readableStream
            .pipe(split()) // This splits the data into 'lines'
            .on('data', function (chunk) {

                readableStream.pause()

                chunkCount++;

                syncFunctionToWriteLogEntryWithCallback( chunk, function() {
                    readableStream.resume();
                } );

            })
            .on('end', function () {
                // resolve the promise which bounds this process
                defer.resolve({v:3,chunkCount: chunkCount})

            });

我认为 MongoDB 在这种情况下不会有重大问题。

因为使用 pause()resume() 似乎有一些问题。我再写一个方案,就是用Transform stream。

var Transform = require('stream').Transform;

var myTransform = new Transform({
   transform(chunk, encoding, cb) {
      chunkCount++;

      syncFunctionToWriteLogEntryWithCallback( chunk, function() {
         cb();
      } );
  },

  flush(cb) {
      chunkCount++;
      syncFunctionToWriteLogEntryWithCallback( chunk, function() {
         cb();
      } );
  }
});

readableStream
        .pipe( split() )
        .pipe( myTransform );

使用转换流允许您在处理完流时提供回调。