1
有沒有辦法獲得hdfs中所有目錄和文件的最後修改時間?我想創建顯示信息的頁面,但是我不知道如何在一個.txt文件中獲取最後一個mod時間。Hadoop目錄/文件的最後修改時間
有沒有辦法獲得hdfs中所有目錄和文件的最後修改時間?我想創建顯示信息的頁面,但是我不知道如何在一個.txt文件中獲取最後一個mod時間。Hadoop目錄/文件的最後修改時間
看看是否有幫助:
public class HdfsDemo {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
FileSystem fs = FileSystem.get(conf);
System.out.println("Enter the directory name : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Path path = new Path(br.readLine());
displayDirectoryContents(fs, path);
fs.close();
}
private static void displayDirectoryContents(FileSystem fs, Path rootDir) {
// TODO Auto-generated method stub
try {
FileStatus[] status = fs.listStatus(rootDir);
for (FileStatus file : status) {
if (file.isDir()) {
System.out.println("DIRECTORY : " + file.getPath() + " - Last modification time : " + file.getModificationTime());
displayDirectoryContents(fs, file.getPath());
} else {
System.out.println("FILE : " + file.getPath() + " - Last modification time : " + file.getModificationTime());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
有一點要注意,雖然,getModificationTime()返回文件的毫秒修改時間自1970年1月1日,UTC。
您可能需要遍歷文件和目錄,以獲取每個路徑的狀態 - 您可以使用下面的代碼(只是示例) - 但我不確定,如果有大量的文件和目錄。
Configuration conf = new Configuration();
conf.set("fs.default.name", "hdfs://<namenod_ip_address:<port>");
conf.set("mapred.job.tracker", "<jobtracker_ip_address>:<port>");
conf.setBoolean("fs.hdfs.impl.disable.cache", true);
FileSystem lfs = FileSystem.get(l_configuration);
fs.getFileStatus(new Path("/your/path")).getModificationTime();