- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要找出使用 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()
代替 watchFile
或 fstatSync
可以做得更好。
为了从文件中读取/写入,我刚刚在下面写了一些有趣的东西:
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/
我用运行 Node node --debug app OR node --debug-brk app 它有反应 debugger listening on port 5858 Express serv
这个问题在这里已经有了答案: What is the difference between (int *i) and (int* i) in context of both C and C++? [
我有一个应用程序,它通过消息队列将数据库写入命令分派(dispatch)给工作人员(数量非常大),因此无法保证它们的接收顺序。 我有两个 Node ,例如“Account”和“Media”。在此假设的
有没有办法在调用 ts-node 时将选项传递给 Node ?我正在尝试在 Node 中使用一个实验性功能,如果它能与 ts-node 一起使用,那就太好了。 这就是我目前正在做的事情: ts-nod
我有一个容器化的Node应用程序,它在DigitalOcean服务器上运行。当我更新服务器上的应用程序时,该应用程序必须关闭一小段时间。为了能够更新应用程序并避免停机,我目前正在阅读零停机时间部署/蓝
我正在编写一个 Node.js 应用程序。我正在使用 request 和 Cheerio 加载一组 URL 并获取该网站的大量信息,现在假设我想要获取的只是标题: var urls = {"url_1
如果不弹出以下错误,我无法安装任何 Node.js 模块。错误代码引用package.json文件。如果知道为什么会发生这种情况,我们将不胜感激。 最佳答案 这些不是错误,它们只是警告。一切都应该如此
如果我运行(从我的项目目录中): supervisor javascripts/index.js 我得到:/usr/bin/env: Node :没有这样的文件或目录 如果我运行: node java
我已遵循使用 Node-Inspector 的所有步骤 但是当我打开应用程序时,我在控制台上看不到任何脚本或日志。 我的应用程序在端口 4000 上运行。我认为唯一可能发生冲突的是端口 8080 上的
我在android中使用rxjava2,有时会遇到这样的问题: Observable.fromArray( // maybe a list about photo url in SD
我目前正在使用 Node 光纤来编写同步服务器端代码。我主要通过 try-catch block 进行错误处理,但外部库或其他小部分异步代码中总是有可能发生错误。我正在考虑使用新的域功能来尝试将这些错
看起来node-debug是node-inspector周围的一个shell?分别什么时候应该使用? 最佳答案 如果您安装node-debug,您只能访问node-debug命令。 如果您安装node
我目前正在代理后面工作,该代理不允许我执行此命令的 HTTP GET 请求阶段: Node node-sass/scripts/build.js 请求阶段: gyp http GET https://
听说node js可以用在服务端。我以前用过jsp。 jsp页面内部的java代码对客户端是不可见的。如果 Node js 只是 javascript,那么它如何对客户端不可见? 最佳答案 首先,No
我正在为 Node native 插件从 node-waf 构建迁移到 node-gyp 构建系统。 node-gyp 说它支持多个目标版本,但我在使用 node-gyp 时找不到如何指定目标 Nod
给定一个 $node ,我正在尝试在以下两种输出该 $node 的方式之间做出决定。 要么 $output = theme('node', $node); 或 node_build_content($
如果package.json中的窗口A打开一个新窗口B,node-main如何访问它?这是我的代码: package.json { "main": "index.html",
我试图在我的 xml 中的特定节点 ( ) 之前插入一个注释节点。这是它的方法: function test(xmlResponse) { var parser = new DOMParse
我正在尝试做npm install wrtc使用 Node 版本 16.14.0 但这还没有完成。它在给npm error code 1所以我试图将 Node 版本更改为以前的 lts 14.19.0
当我在 Visual Studio 中运行 Node.js 应用程序时,我收到以下消息:DeprecationWarning: 'node --debug' 和 'node --debug-brk'
我是一名优秀的程序员,十分优秀!