Read nth line in Node.js without reading entire fi

2020-03-29 04:54发布

I'm trying to use Node.js to get a specific line for a binary search in a 48 Million line file, but I don't want to read the entire file to memory. Is there some function that will let me read, say, line 30 million? I'm looking for something like Python's linecache module.

Update for how this is different: I would like to not read the entire file to memory. The question this is identified as a duplicate of reads the entire file to memory.

标签: node.js file
2条回答
迷人小祖宗
2楼-- · 2020-03-29 05:20

You should use readline module from Node’s standard library. I deal with 30-40 million rows files in my project and this works great.

If you want to do that in a less verbose manner and don’t mind to use third party dependency use nthline package:

const nthline = require('nthline')
    , filePath = '/path/to/100-million-rows-file'
    , rowNumber = 42

nthline(rowNumber, filePath)
  .then(line => console.log(line))
查看更多
三岁会撩人
3楼-- · 2020-03-29 05:38

According to the documentation, you can use fs.createReadStream(path[, options]), where:

options can include start and end values to read a range of bytes from the file instead of the entire file.

Unfortunately, you have to approximate the desired position/line, but it seems to be no seek like function in node js.

EDIT

The above solution works well with lines that have fixed length.

New line character is nothing more than a character like all the others, so looking for new lines is like looking for lines that start with the character a.
Because of that, if you have lines with variable length, the only viable approach is to load them one at a time in memory and discard those in which you are not interested.

查看更多
登录 后发表回答