Ishan
Ishan

Reputation: 154

How can I get HDFS free space in Java code?

To get available free space in HDFS, I can use following command:

hadoop fs -df /

But I need to access it using Java code. The Hadoop Filesystem API doesn't seem to give free hdfs space.

I could find the java equivalent of df command but was not able to understand how to use it.

Can anyone plese help?

Upvotes: 0

Views: 357

Answers (1)

Ishan
Ishan

Reputation: 154

After studying the code of how the command works I found out that it uses Hadoop FileSystem API to fetch an FsStatus object. This object gives used and free hdfs space.

The code to fetch this can be:

    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(conf);

    Path path = new Path("/");
    long used = fs.getStatus(path).getUsed();
    long available = fs.getStatus(path).getRemaining();

Upvotes: 1

Related Questions