0

我正在请求一个包含 150K 记录的大文件,但它会引发“toString failed”错误。nodejs/node#3175说这是因为 maxBufferSize。请求适用于 200 条记录,但它是一个外部 api,要求是一次获取所有记录。[那里没有分页 :( ] 有没有办法为这个请求设置缓冲区大小?

我已经在这里问过这个问题

编辑:

request("http://www.site-containing-big-data/api",
        function (error, response, body) {
            console.log('got something to show');
            if(!error && response.statusCode == 200) {
                resolve(body);
            }else if(error){
                reject(error);
            }
        });

但除了toString failed消息之外,控制台中没有显示任何内容

4

1 回答 1

0

解决了。这是一个 xml 文件,我试图直接处理它。现在我首先保存在一个文件中,然后使用xml-stream它来一个一个地处理每个对象。

request.get("http://www.site-containing-big-data/api" )
        .on('error', function(errReq) {
            console.log('error while reading from big site : ', errReq);

        }).on('end', function(){
            console.log('got from big site, now processing');
            var XmlStream = require('xml-stream') ;
            var stream=fs.createReadStream('bigfile.xml');
            xml = new XmlStream(stream);
            xml.on('endElement: item', function(item) {//item is a node. structure is <items><item></item><item></item></items>
                //do something here
            });
            xml.on('end', function(){
                // when processing finished for all objects/items in that file
                });
            });
        })
        .pipe(fs.createWriteStream('bigfile.xml'));
于 2016-02-12T12:38:13.583 回答