hadoop目录/文件上次修改次数

hyrbngr7  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(338)

有没有办法得到hdfs中所有目录和文件的最后修改时间?我想创建一个显示信息的页面,但是我不知道如何在一个.txt文件中获取最后的mod时间。

hmtdttj4

hmtdttj41#

您可能需要遍历文件和目录,以获得每个路径的状态—您可以使用下面的代码(只是示例)—但我不确定,如果您有大量的文件和目录,那么效率会有多高。

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

m528fe3b2#

看看是否有帮助:

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以来文件的修改时间(以毫秒为单位)。

相关问题