栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Nodejs中解析大型JSON文件并独立处理每个对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Nodejs中解析大型JSON文件并独立处理每个对象

有一个名为“ stream-json”的不错的模块,它可以完全满足您的需求。

它可以解析远远超出可用内存的JSON文件。

StreamArray处理一个频繁的用例:与Django生产的数据库转储类似的大量相对较小的对象。它逐个流处理阵列组件,并自动进行组装。

这是一个非常基本的示例:

const StreamArray = require('stream-json/streamers/StreamArray');const path = require('path');const fs = require('fs');const jsonStream = StreamArray.withParser();//You'll get json objects here//Key is an array-index herejsonStream.on('data', ({key, value}) => {    console.log(key, value);});jsonStream.on('end', () => {    console.log('All done');});const filename = path.join(__dirname, 'sample.json');fs.createReadStream(filename).pipe(jsonStream.input);

如果您想做一些更复杂的事情,例如按顺序处理一个对象(保持顺序)并为每个对象应用一些异步操作,则可以执行以下自定义Writeable流:

const StreamArray = require('stream-json/streamers/StreamArray');const {Writable} = require('stream');const path = require('path');const fs = require('fs');const fileStream = fs.createReadStream(path.join(__dirname, 'sample.json'));const jsonStream = StreamArray.withParser();const processingStream = new Writable({    write({key, value}, encoding, callback) {        //Save to mongo or do any other async actions        setTimeout(() => { console.log(value); //Next record will be read only current one is fully processed callback();        }, 1000);    },    //Don't skip this, as we need to operate with objects, not buffers    objectMode: true});//Pipe the streams as followsfileStream.pipe(jsonStream.input);jsonStream.pipe(processingStream);//So we're waiting for the 'finish' event when everything is done.processingStream.on('finish', () => console.log('All done'));

请注意: 以上示例已针对“ stream-json@1.1.3”进行了测试。对于某些以前的版本(可能早于1.0.0),您可能必须:

const StreamArray = require('stream-json/utils/StreamArray');

然后

const jsonStream = StreamArray.make();



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号