By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .

Background

Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem/FileContext interface for HDFS.

Thus, we could use these web APIs to perform HDFS operations in other programming language like C#.

WebHDFS APIs reference

https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/WebHDFS.html

Examples

List files

The following code snippet retrieve the file list in the root directory in my local Hadoop node.:

static void Main(string[] args)
         {
             WebHdfsListStatusApi();
             Console.ReadLine();
         }

        static void WebHdfsListStatusApi()
         {

            var protocal = "http";
             var host = "127.0.0.1";
             var port = 9870;
             var hdfsFilePath = "\\";
             var operation = "LISTSTATUS";
             var url = $"{protocal}://{host}:{port}/webhdfs/v1/{hdfsFilePath}?op={operation}";
             var request = (HttpWebRequest)WebRequest.Create(url);
             var response = (HttpWebResponse)request.GetResponse();
             using (StreamReader reader = new StreamReader(response.GetResponseStream()))
             {
                 var result = reader.ReadToEnd();
                 Console.WriteLine(result);
             }
         }

The output looks like the following screenshot:

image

The following is the output in Postman:

image

Get file content

Similarly you can also get the content of a file through OPEN operation:

static void Main(string[] args)
         {
             WebHdfsGetFileContent();
             Console.ReadLine();
         }

        static void WebHdfsGetFileContent()
         {

            var protocal = "http";
             var host = "127.0.0.1";
             var port = 9870;
             var hdfsFilePath = "\\Sales.csv";
             var operation = "OPEN";
             var url = $"{protocal}://{host}:{port}/webhdfs/v1/{hdfsFilePath}?op={operation}";
             var request = (HttpWebRequest)WebRequest.Create(url);
             var response = (HttpWebResponse)request.GetResponse();
             using (StreamReader reader = new StreamReader(response.GetResponseStream()))
             {
                 var result = reader.ReadToEnd();
                 Console.WriteLine(result);
             }
         }

The following screenshot is the sample output:

image

info Last modified by Raymond at 3 years ago * This page is subject to Site terms.

More from Kontext

local_offer jupyter-notebook local_offer hdfs

visibility 24
thumb_up 0
access_time 24 days ago

Jupyter notebook service can be started in most of operating system. In the system where Hadoop clients are available, you can also easily ingest data into HDFS (Hadoop Distributed File System) using HDFS CLIs.  *Python 3 Kernel is used in the following examples. List files in H...

open_in_new View open_in_new Code snippets

local_offer hdfs local_offer hadoop local_offer windows

visibility 59
thumb_up 0
access_time 1 month ago

Network Attached Storage are commonly used in many enterprises where files are stored remotely on those servers.  They typically provide access to files using network file sharing protocols such as  ...

open_in_new View open_in_new Hadoop

local_offer hive local_offer hdfs

visibility 60
thumb_up 0
access_time 2 months ago

In Hive, there are two types of tables can be created - internal and external table. Internal tables are also called managed tables. Different features are available to different types. This article lists some of the common differences.  Internal table By default, Hive creates ...

open_in_new View open_in_new Hadoop

Schema Merging (Evolution) with Parquet in Spark and Hive

local_offer parquet local_offer pyspark local_offer spark-2-x local_offer hive local_offer hdfs

visibility 297
thumb_up 0
access_time 3 months ago

Schema evolution is supported by many frameworks or data serialization systems such as Avro, Orc, Protocol Buffer and Parquet. With schema evolution, one set of data can be stored in multiple files with different but compatible schema. In Spark, Parquet data source can detect and merge sch...

open_in_new View open_in_new Spark + PySpark

info About author

Kontext dark theme mode

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward
Kontext Column

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.

Learn more arrow_forward
info Follow us on Twitter to get the latest article updates. Follow us