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

About C# Programming Language: features, and syntax and so on.

rss_feed Subscribe RSS

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 2 years ago
info About author

info License/Terms

More from Kontext

local_offer windows10 local_offer hadoop local_offer hdfs

visibility 6
comment 0
thumb_up 0
access_time 6 hours ago

Issue When installing Hadoop 3.2.1 on Windows 10,  you may encounter the following error when trying to format HDFS  namnode: ERROR namenode.NameNode: Failed to start namenode. The error happens when running the following comm...

open_in_new View

Compile and Build Hadoop 3.2.1 on Windows 10 Guide

local_offer windows10 local_offer hadoop

visibility 71
comment 0
thumb_up 1
access_time 5 days ago

This article provides detailed steps about how to compile and build Hadoop (incl. native libs) on Windows 10. The following guide is based on Hadoop release 3.2.1. ...

open_in_new View

Latest Hadoop 3.2.1 Installation on Windows 10 Step by Step Guide

local_offer windows10 local_offer hadoop local_offer yarn

visibility 63
comment 0
thumb_up 0
access_time 7 days ago

This detailed step-by-step guide shows you how to install the latest Hadoop (v3.2.1) on Windows 10. It also provides a temporary fix for bug HDFS-14084 (java.lang.UnsupportedOperationException INFO).

open_in_new View

local_offer spark local_offer hadoop local_offer pyspark local_offer oozie local_offer hue

visibility 869
comment 0
thumb_up 0
access_time 6 months ago

When submitting Spark applications to YARN cluster, two deploy modes can be used: client and cluster. For client mode (default), Spark driver runs on the machine that the Spark application was submitted while for cluster mode, the driver runs on a random node in a cluster. On this page, I am goin...

open_in_new View

comment Comments (0)
Please log in or register to comment. account_circle Log in person_add Register
No comments yet.