hdfs get size of directory java

hdfs get size of directory java

To get the size of a directory in HDFS (Hadoop Distributed File System) using Java, you can use the FileSystem class and the ContentSummary class from the org.apache.hadoop.fs package.

Here's an example of how you can get the size of a directory in HDFS using Java:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.ContentSummary;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Main {
    public static void main(String[] args) throws Exception {
        // create a configuration object
        Configuration conf = new Configuration();

        // get the filesystem object
        FileSystem fs = FileSystem.get(conf);

        // get the path of the directory
        Path dirPath = new Path("/path/to/directory");

        // get the content summary of the directory
        ContentSummary contentSummary = fs.getContentSummary(dirPath);

        // get the size of the directory in bytes
        long size = contentSummary.getLength();

        // print the size of the directory
        System.out.println("Size of directory: " + size + " bytes");
    }
}
Sou‮‬rce:www.lautturi.com

This code creates a Configuration object and a FileSystem object using the FileSystem.get() method. It then creates a Path object for the directory you want to get the size of. It uses the getContentSummary() method of the FileSystem object to get the ContentSummary of the directory, and then gets the size of the directory in bytes using the getLength() method of the ContentSummary object. Finally, it prints the size of the directory to the console.

Note that this code assumes that the HDFS cluster is configured and running, and that you have the necessary permissions to access the directory. You may also need to set up the HADOOP_CONF_DIR environment variable or specify the path to the Hadoop configuration files in the Configuration object.

For more information about the FileSystem and ContentSummary classes, and other HDFS APIs in Java, you can refer to the Hadoop documentation (https://hadoop.apache.org/docs/stable/).

Created Time:2017-11-01 12:05:05  Author:lautturi