Hadoop dir/file last modification times

前端 未结 2 663
时光取名叫无心
时光取名叫无心 2021-01-23 07:09

Is there a way to get the last modified times of all dirs and files in hdfs? I want to create page that displays the information, but I have no clue how to go about getting the

2条回答
  •  后悔当初
    2021-01-23 07:47

    See if it helps :

    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();
            }
        }
    }
    

    One thing to notice though, getModificationTime() returns the modification time of file in milliseconds since January 1, 1970 UTC.

提交回复
热议问题