NodeJS Async JSON parsing causing Buffer.toString() failure
我正试图解析nodejs中相当大的JSON文件(~500MB)。我的实现基于以下答案中给出的异步方法:
1 2 3 4 5 6 7 8 9 | var fileStream = require('fs'); var jsonObj; fileStream.readFile('./data/exporttest2.json', fileCallback); function fileCallback (err, data) { return err ? (console.log(err), !1):(jsonObj = JSON.parse(data)); //Process JSON data here } |
这一切都很好,但我收到以下错误消息:
1 2 3 4 5 6 7 8 9 10 11 12 | buffer.js:495 throw new Error('"toString()" failed'); ^ Error:"toString()" failed at Buffer.toString (buffer.js:495:11) at Object.parse (native) at fileCallback (C:\Users\1700675 esearch\Experiments odeJS f_EU.js:49:18) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3) |
我从这个答案中了解到,这是由于V8发动机的最大缓冲长度设置为256MB造成的。
那么,我的问题是,有没有一种方法可以在不超过256MB的缓冲区长度的情况下异步读取JSON文件,而不需要手动将JSON数据传播到多个文件中?
is there a way I can asynchronously read my JSON file in chunks that do not exceed the buffer length of 256Mb, without manually disseminating my JSON data into several files?
这是一个常见问题,有几个模块可以帮助您:
- https://www.npmjs.com/package/jsonstream
- https://www.npmjs.com/package/stream-json
- https://www.npmjs.com/package/json-stream
- https://www.npmjs.com/package/json-parse-stream
- https://www.npmjs.com/package/json-streams
- https://www.npmjs.com/package/jsonparse网站
jsonstream示例:
1 2 3 4 5 | const JSONStream = require('JSONStream'); const fs = require('fs'); fs.createReadStrem('./data/exporttest2.json') .pipe(JSONStream.parse('...'))... |
有关所有参数的详细信息,请参阅文档。
尝试使用流:
1 2 3 4 5 6 7 8 9 10 | let fs = require("fs"); let s = fs.createReadStream('./a.json'); let data = []; s.on('data', function (chunk) { data.push(chunk); }).on('end', function () { let json = Buffer.concat(data).toString(); console.log(JSON.parse(json)); }); |