我使用10行测试文件在本地计算机上运行正常的脚本。但是,当我在AWS中使用带有 2000万行的真实文件的脚本时,我似乎失去了1/5的行。我在这个剧本中做错了什么?我没有收到任何错误消息。
var sha1 = require('sha1');
var fs = require('fs');
var lineReader = require('readline').createInterface({
terminal: false,
input: require('fs').createReadStream('./readme.txt')
});
lineReader.on('line', function (line) {
var hashed = sha1(line);
fs.appendFile('./writeme.txt', line + "," + hashed + "\n", function(err){
})
});
答案 0 :(得分:1)
这是最终奏效的。谢谢event-stream和Transform选项!
var fs = require('fs');
var stream = require('stream');
var es = require('event-stream');
var sha1 = require('sha1');
var input = fs.createReadStream("readme.txt");
var output = fs.createWriteStream("writeme.txt");
var parse = new stream.Transform;
parse._transform = function(chunk, encoding, done) {
var hashed = sha1(chunk);
var line = chunk + ' | ' + hashed + '\n';
done(null, line);
}
input.pipe(es.split('\n')).pipe(parse).pipe(output);