gpt4 book ai didi

javascript - 使用 Node.js 实时读取文件

转载 作者:IT老高 更新时间:2023-10-28 23:10:37 27 4
gpt4 key购买 nike

我需要找出使用 node.js 实时读取写入文件的数据的最佳方法。麻烦的是,Node 是一艘快速移动的船,这使得找到解决问题的最佳方法变得困难。

我想做的事
我有一个 java 进程正在做某事,然后将它所做的事情的结果写入文本文件。它通常需要 5 分钟到 5 小时的时间来运行,并且一直在写入数据,并且可以达到相当高的吞吐率(大约 1000 行/秒)。

我想实时读取此文件,然后使用 Node 聚合数据并将其写入可以在客户端上绘制图形的套接字。

客户端、图形、套接字和聚合逻辑都已完成,但我对读取文件的最佳方法感到困惑。

我尝试过(或至少玩过)的东西
FIFO - 我可以告诉我的 Java 进程写入 fifo 并使用 Node 读取它,这实际上是我们目前使用 Perl 实现的方式,但是因为其他一切都在 Node 中运行,所以它是有意义的将代码移植过来。

Unix Sockets - 同上。

fs.watchFile - 这能满足我们的需要吗?

fs.createReadStream - 这比 watchFile 好吗?

fs & tail -f - 看起来像个 hack。

实际上,我的问题是什么
我倾向于使用 Unix 套接字,这似乎是最快的选择。但是node是否有更好的内置功能来实时从fs读取文件?

最佳答案

如果您想将文件作为数据的持久存储来防止在系统崩溃或正在运行的进程网络中的某个成员死亡的情况下丢失流,您仍然可以继续写入文件并从中读取。

如果您不需要此文件作为 Java 进程生成的结果的持久存储,那么使用 Unix 套接字在易用性和性能方面都会好得多。

fs.watchFile() 不是您所需要的,因为它适用于文件系统报告的文件统计信息,并且由于您想读取已经写入的文件,所以这不是您需要的想要。

简短更新:我很遗憾地意识到,虽然我在上一段中指责 fs.watchFile() 使用文件统计信息,但我也做了同样的事情在下面的示例代码中自己做事!虽然我已经警告读者“小心!”因为我只用了几分钟就写好了,甚至没有测试好;不过,如果底层系统支持,使用 fs.watch() 代替 watchFilefstatSync 可以做得更好。

为了从文件中读取/写入,我刚刚在下面写了一些有趣的东西:

test-fs-writer.js:[您不需要这个,因为您在 Java 进程中编写文件]

var fs = require('fs'),
lineno=0;

var stream = fs.createWriteStream('test-read-write.txt', {flags:'a'});

stream.on('open', function() {
console.log('Stream opened, will start writing in 2 secs');
setInterval(function() { stream.write((++lineno)+' oi!\n'); }, 2000);
});

test-fs-reader.js:[注意,这只是演示,检查错误对象!]

var fs = require('fs'),
bite_size = 256,
readbytes = 0,
file;

fs.open('test-read-write.txt', 'r', function(err, fd) { file = fd; readsome(); });

function readsome() {
var stats = fs.fstatSync(file); // yes sometimes async does not make sense!
if(stats.size<readbytes+1) {
console.log('Hehe I am much faster than your writer..! I will sleep for a while, I deserve it!');
setTimeout(readsome, 3000);
}
else {
fs.read(file, new Buffer(bite_size), 0, bite_size, readbytes, processsome);
}
}

function processsome(err, bytecount, buff) {
console.log('Read', bytecount, 'and will process it now.');

// Here we will process our incoming data:
// Do whatever you need. Just be careful about not using beyond the bytecount in buff.
console.log(buff.toString('utf-8', 0, bytecount));

// So we continue reading from where we left:
readbytes+=bytecount;
process.nextTick(readsome);
}

您可以安全地避免使用 nextTick 并直接调用 readsome()。由于我们仍在此处进行同步,因此在任何意义上都没有必要。我只是喜欢它。 :p

编辑 Oliver Lloyd

以上面的例子为例,但将其扩展为读取 CSV 数据给出:

var lastLineFeed,
lineArray;
function processsome(err, bytecount, buff) {
lastLineFeed = buff.toString('utf-8', 0, bytecount).lastIndexOf('\n');

if(lastLineFeed > -1){

// Split the buffer by line
lineArray = buff.toString('utf-8', 0, bytecount).slice(0,lastLineFeed).split('\n');

// Then split each line by comma
for(i=0;i<lineArray.length;i++){
// Add read rows to an array for use elsewhere
valueArray.push(lineArray[i].split(','));
}

// Set a new position to read from
readbytes+=lastLineFeed+1;
} else {
// No complete lines were read
readbytes+=bytecount;
}
process.nextTick(readFile);
}

关于javascript - 使用 Node.js 实时读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11225001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com