Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need suggestion on my approach : to read a file which is being written continuously?

Tags:

java

file

io

I have one csv file, which is being written continuously by script. It writes timestamp and some other data per row. I have to read the latest data first. Currently I am using RandomAccessFile in java to read the file in reverse way. But as its written continuously, I have to read the new data with priority. I am maintaining which timestamp has been sent and doing the work. It results unnecessary scanning operations.

Is there any better way to deal with this scenario?

Thanks in advance,

like image 643
turbo Avatar asked Nov 06 '22 05:11

turbo


1 Answers

You could consider having one thread that reads new lines as they appear and pushes them onto a stack of unprocessed rows, and a second thread that pops the stack and processes the new rows in reverse order.

Depending on how long it takes to process a new row compared to how quickly they are generated, this might be sufficient. If new rows are generated faster than you can process them then this approach probably won't work - the stack will get too big and you'll run out of memory. In that case, depending on your requirements, you might be able to get away with a size-limited stack that discards old entries.

like image 152
Cameron Skinner Avatar answered Nov 09 '22 14:11

Cameron Skinner