4

我想使用org.apache.hadoop.fs.FileSystemAPI 以编程方式跟踪 hdfs 文件。有没有办法以等同于hadoop fs -tail -f命令的方式使用 API 来跟踪文件?

4

1 回答 1

3

也许我误解了这个问题。hadoop fs -tail -f是不是使用 API 实现的?

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;
  }
}
于 2011-10-27T09:11:05.170 回答