我正在尝试学习javascript流并尝试将线条转换为对象。这里我通过getObject从s3文件流式传输,我能够将行转换为json格式,但是,我无法看到为readline触发的close
事件。
var aws = require('aws-sdk');
var readline = require('readline');
var path = require('path');
var fs = require('fs');
var outstream = new (require('stream'))()
var s3 = new aws.S3({
apiVersion: "2006-03-01",
accessKeyId: "foobar",
secretAccessKey: "foobar"
});
var readParams = {
Bucket : "bucket", // name of the bucket
Key: "key" // exact file name
}
var readStream = s3.getObject(readParams).createReadStream();
var lineReader = readline.createInterface({
input: readStream,
output: outstream
});
var record = {};
var result = [];
lineReader.on('line', function(line) {
var lineStr = line.toString();
if (lineStr.includes("EOE")) {
result.push(record);
record = {};
lineReader.pause();
} else {
lineReader.resume();
}
var strArr = lineStr.split("=");
var key = strArr[0];
var value = strArr[1];
if(key === 'RequestID' || key === 'StartTime' || key === 'Time' || key === 'Operation') {
value = value.trim().replace(" ms", "");
record[[key]] = value;
}
}).on('close', function() {
console.log("Finished");
});
当我完成流式传输s3 get请求时,我无法看到“已完成”。谢谢你的帮助
答案 0 :(得分:2)
我通过删除linereader.pause()
和linereader.resume()
行来解决了这个问题。如果行包含EOE暂停读取器并跳过该行,我认为暂停和恢复的方式正常。如果行不包含EOE,则处理记录并继续。所以基本上这里是工作代码。
lineReader.on('line', function(line) {
var lineStr = line.toString();
if (lineStr.includes("EOE")) {
result.push(record);
record = {};
} else {
var strArr = lineStr.split("=");
var key = strArr[0];
var value = strArr[1];
if(key === 'RequestID' || key === 'StartTime' || key === 'Time' || key === 'Operation') {
value = value.trim().replace(" ms", "");
record[[key]] = value;
}
}
}).on('close', function() {
console.log("Finished");
});
感谢所有帮助!