简体   繁体   English

如何从NodeJS中的文本文件提取(读取+删除)?

[英]How can I extract (read + delete) from a textfile in NodeJS?

I'm building a script that reads log files, handles what needs to be handled then writes them to a database 我正在构建一个脚本,该脚本读取日志文件,处理需要处理的内容,然后将其写入数据库

Some caveats : 一些警告:

Some log files have a lot of input, multiple times a second Some log files have few to no input at all 一些日志文件每秒输入很多次,很多日志文件很少甚至根本没有输入

What I try in simple words: 我用简单的话尝试一下:

Reading the first line of a file, then deleting this line to go to the next one, while I handle the first line, other lines could be added.. 读取文件的第一行,然后删除此行以转到下一行,而当我处理第一行时,可以添加其他行。

Issues I'm facing 我面临的问题

  1. When I try reading a file then processing it, then deleting the files, some lines have been added 当我尝试读取文件然后处理它,然后删除文件时,已经添加了一些行
  2. When the app crashes while handling multiple lines at once for any reason, I can't know what lines have been processed. 当应用程序由于某种原因同时处理多行崩溃时,我不知道已经处理了哪些行。

Tried so far 到目前为止尝试过

fs.readdir('logs/', (err, filenames) => {
filenames.forEach((filename) => {
  fs.readFile('logs/'+filename, 'utf-8', (err, content) => {

    //processing all new lines (can take multiple ms)

    //deleting file
    fs.unlink('logs/'+filename)
  });
});

}); });

Is there not a (native or not) method to 'take' first line(s), or take all lines, from a file at once? 是否没有(原生或非原生)方法来一次“取走”文件的第一行或取走所有行?

Something similar to what the Array.shift() method does to arrays.. 类似于Array.shift()方法对数组所做的操作。

If your log files has been writen as rotate logs. 如果您的日志文件已被写入为轮转日志。 Example: Each hours has each log file, 9AM.log, 10AM.log....When you process the log files, you can skip current file and process another files. 示例:每个小时都有每个日志文件9 AM.log、10AM.log...。当您处理日志文件时,可以跳过current文件并处理另一个文件。 ex: now is 10:30 AM o'clock, skip file 10AM.log, solve another files. 例如:现在是上午10:30,请跳过文件10 AM.log,解决另一个文件。

Why you are reading the file at once. 为什么要一次读取文件。 Instead you can use the node.js streams . 相反,您可以使用node.js streams https://nodejs.org/api/fs.html#fs_class_fs_readstream https://nodejs.org/api/fs.html#fs_class_fs_readstream

This will read the files and output to console 这将读取文件并输出到控制台

var fs = require('fs');
var readStream = fs.createReadStream('myfile.txt');
readStream.pipe(process.stdout);

You can also go for the npm package node-tail to read the content of a files while new content written to it. 您还可以使用npm软件包node-tail读取文件的内容,同时将新内容写入其中。 https://github.com/lucagrulla/node-tail https://github.com/lucagrulla/node-tail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM