node.js - Nodejs 读取非常大的文件(~10GB),逐行处理然后写入其他文件

标签 node.js file-io large-files file-handling file-processing

我有一个特定格式的 10 GB 日志文件,我想逐行处理这个文件,然后将输出写入其他文件 在应用一些转换 之后。我正在使用 Node 进行此操作。

虽然这个方法很好,但是要花很多时间。我能够在 30-45 分钟内在 JAVA 中完成此操作,但在 Node 中完成相同的工作需要 160 多分钟。以下是代码:

Following is the initiation code which reads each line from the input.

var path = '../10GB_input_file.txt';
var output_file = '../output.txt';

function fileopsmain(){

    fs.exists(output_file, function(exists){
        if(exists) {
            fs.unlink(output_file, function (err) {
                if (err) throw err;
                console.log('successfully deleted ' + output_file);
            });
        }
    });

    new lazy(fs.createReadStream(path, {bufferSize: 128 * 4096}))
        .lines
        .forEach(function(line){
            var line_arr = line.toString().split(';');
            perform_line_ops(line_arr, line_arr[6], line_arr[7], line_arr[10]);
        }
    );

}

This is the method that performs some operation over that line and passes the input to write method to write it into the output file.

function perform_line_ops(line_arr, range_start, range_end, daynums){

    var _new_lines = '';
    for(var i=0; i<days; i++){
        //perform some operation to modify line pass it to print
    }

    write_line_ops(_new_lines);
}

Following method is used to write data into a new file.

function write_line_ops(line) {
    if(line != null && line != ''){
        fs.appendFileSync(output_file, line);
    }
}

我想把这个时间缩短到 15-20 分钟。是否可以这样做。

另请注意,我正在带有 8 GB RAM 的英特尔 i7 处理器 上进行尝试。

最佳答案

您无需模块即可轻松完成此操作。例如:

var fs = require('fs');
var inspect = require('util').inspect;

var buffer = '';
var rs = fs.createReadStream('foo.log');
rs.on('data', function(chunk) {
  var lines = (buffer + chunk).split(/\r?\n/g);
  buffer = lines.pop();
  for (var i = 0; i < lines.length; ++i) {
    // do something with `lines[i]`
    console.log('found line: ' + inspect(lines[i]));
  }
});
rs.on('end', function() {
  // optionally process `buffer` here if you want to treat leftover data without
  // a newline as a "line"
  console.log('ended on non-empty buffer: ' + inspect(buffer));
});

关于node.js - Nodejs 读取非常大的文件(~10GB),逐行处理然后写入其他文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31479379/

相关文章:

java - 从类路径资源(XML 文件)获取输入流

algorithm - 从大文件中删除重复项

node.js - Node.js 终端中漂亮的异常消息

jquery - 如何在 PhantomJS 中使用 jQuery 循环表行

node.js - 将 ReadableStream 图像写入文件

.net - 在 Linux 世界中是否有等同于 .Net FileSystemWatcher 的东西?

node.js - 使用 Angular $http 请求时,passport req.isAuthenticated 始终返回 false

node.js - Node js Monk 更新持续存在

android - 文件下载 - 内存不足(OOM)

python:从压缩文本文件中读取行