Programmatic equivalent of 'hadoop fs -tail -f'
I want to tail an hdfs file programmatically using the org.apache.hadoop.fs.FileSystem
API.
Is there a way to tail the file using the API in a way which is equivalent to hadoop fs开发者_JS百科 -tail -f
command?
Maybe I misunderstand the question. hadoop fs -tail -f
is implemented using the API isn't it?
From org.apache.hadoop.fs.FsShell.tail(String[], int)
long fileSize = srcFs.getFileStatus(path).getLen();
long offset = (fileSize > 1024) ? fileSize - 1024: 0;
while (true) {
FSDataInputStream in = srcFs.open(path);
in.seek(offset);
IOUtils.copyBytes(in, System.out, 1024, false);
offset = in.getPos();
in.close();
if (!foption) {
break;
}
fileSize = srcFs.getFileStatus(path).getLen();
offset = (fileSize > offset) ? offset: fileSize;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
break;
}
}
精彩评论