node.js - 如何在nodejs中向后读取大型json文件

标签 node.js large-files

一种从上到下逐行读取文件的简单方法:

const { once } = require('events');
const { createReadStream } = require('fs');
const { createInterface } = require('readline');

let storeProducts = async () => {
    try {
        const filename = path.join(__dirname, "bulk-101638307937.jsonl");
        const rl = createInterface({
          input: createReadStream(filename),
          crlfDelay: Infinity
        });
    
        rl.on('line', (line) => {
          // Process the line.
          console.log({line: JSON.parse(line)});
        });
    
        await once(rl, 'close');
    
        console.log('File processed.');
      } catch (err) {
        console.error(err);
    }
}
这很好用。但是我需要逐行向后读取一个大的 jsonl 文件 .我试过 this .它向后读取文件,但不是逐行读取。任何帮助,将不胜感激。
一个示例 jsonl
{"id": 1, "product": "A"}
{"variant_id": 1, "product": "A"}
{"id": 2, "product": "B"}
{"variant_id": 2, "product": "B"}
{"id": 3, "product": "C"}
{"variant_id": 3, "product": "C"}
{"id": 4, "product": "D"}
{"variant_id": 4, "product": "D"}
{"variant_id": 5, "product": "D"}

最佳答案

图书馆fs-reverse应该做的伎俩:

const fsR = require('fs-reverse');
const filename = './sample.jsonl';

const readStream = fsR(filename, {})

readStream.on('data', (line) => {
  if(line) { // have this check to make sure empty lines are not parsed
    console.log(JSON.parse(line))
  }
})

关于node.js - 如何在nodejs中向后读取大型json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63666544/

相关文章:

node.js - 如何使用 Slack 中的链接将其深层链接回我的 Slack 机器人?

node.js - vue-loader : Module was not found

java - XML:处理大数据

java - 部分加载具有不同编码的大文本文件

java - Antlr 解析的输入的最大大小

c - 如何可移植地打开大文件支持?

javascript - 通过根据需要旋转数据库或模拟来进行测试?

javascript - 离线 DynamoDB + Serverless + Lambda ResourceNotFoundException

node.js - module.exports 返回值未定义

diff - 内存耗尽: for large files using diff