How to get the HDFS file size using WebHDFS?

WebHDFS

WebHDFS is a protocol which is based on an industry-standard RESTful mechanism. It provides the same functionality as HDFS, but over a REST interface. It uses Kerberos (SPNEGO) and Hadoop delegation tokens for authentication. Few usages of WebHDFS as below

  • User can connects HDFS from the outside cluster.
  • User can interact with the data stored in HDFS from the outside cluster.
  • It is providing web services access to all Hadoop components.
  • It permits clients to access Hadoop from multiple languages without installing Hadoop. We can use common tools like curl/wget to access HDFS.
  • The file read and file write calls are redirected to the corresponding datanodes.

Curl command to get HDFS file size using WebHDFS

Since the WebHDFS is a kind of RESTAPI , we can use CURL command to access it. Lets assume that we have a Hive table in HDFS. As we know that the Hive table data is stored in the HDFS file, we are using WebHDFS to get the size of that file.

In this example, We are going to submit the HTTP GET request to get the content summary of the HDFS file/directory.

Syntax of CURL command

Here the -L option instructs curl to follow any redirect until it reaches the final destination. HDFS uses Kerberos SPNEGO for authentication. To activate (SPNEGO) authentication using the given user name, we have used –negotiate option in curl.

After that, we need to mention the -u: followed by user name. Then we need to mention the host address with the port numebr where the name node is running. Finally we have to give /webhdfs/v1/ followed by the HDFS file path with ?op=GETCONTENTSUMMARY.

Example of webhdfs using curl command:

Hive table used in this example is prime_customers which is resides in the banking database. First we are going to get the location of that table using Desc formatted command in Hive as below.

Also we know the host name and port number to call the API of WebHDFS as below

Lets write the CURL command with all these details to get the HDFS file size.

After running this curl command in the server, we got the HDFS file details in JSON format as below.

Here the length is the number of bytes used by the content. So this length is used as the size of the HDFS file. As it is in byte value, we can apply some formula to convert this into KB,GB,TB values. Instead of CURL command, we can write a Java program to perform all these operations outside the HDFS cluster. In addition, webhdfs used to perform various operations on HDFS File. Please check here for more details.

Recommended Articles