This article mainly introduces the ASP. NET implementation of Hadoop additions and deletions of the sample code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
This article describes the ASP. NET implementation of Hadoop additions and deletions of the sample code, share to everyone, specifically as follows:
Packages.config
<?xml version= "1.0" encoding= "Utf-8"?><packages> <package id= "Microsoft.AspNet.WebApi.Client" version= "4.0.20505.0" targetframework= "net46"/> <package id= "MICROSOFT.DATA.EDM" version= "5.2.0" targetframework= "Net46"/> <package id= "Microsoft.Data.OData" version= "5.2.0" targetframework= "Net46"/> <package id= "Microsoft.Hadoop.WebClient" version= "0.12.5126.42915" targetframework= "net46"/> <package id= " Microsoft.Net.Http "version=" 2.0.20505.0 "targetframework=" net46 "/> <package id=" Microsoft.WindowsAzure.ConfigurationManager "version=" 1.8.0.0 "targetframework=" net46 "/> <package id=" Newtonsoft.json "version=" 4.5.11 "targetframework=" net46 "/> <package id=" system.spatial "version=" 5.2.0 " targetframework= "Net46"/> <package id= "windowsazure.storage" version= "2.0.4.1" targetframework= "Net46"/> </packages>
HDFSAccess.cs
Using microsoft.hadoop.webhdfs;using system;using system.collections.generic;using system.linq;using System.net.http;namespace physical{public sealed class Hdfsaccess {private readonly webhdfsclient webhdfsclient; Public hdfsaccess (String uristring, String userName) {this.webhdfsclient = new webhdfsclient (new Uri (uristring) , userName); } Public list<string> GetDirectories (string path) {var directorystatus = This.webHDFSClient.GetDirectory Status (Path). Result; return DirectoryStatus.Directories.Select (d = d.pathsuffix). ToList (); } Public list<string> GetFiles (string path) {var directorystatus = This.webHDFSClient.GetDirectoryStatus (path). Result; return DirectoryStatus.Files.Select (d = d.pathsuffix). ToList (); When the public bool CreateDirectory (string path) {//incoming path does not contain a root directory, the preset will return under the root directory "/" This.webHDFSClient.CreateD Irectory (Path). Result; } public bool DeleteDirectory (string path) { When an incoming path does not contain a root directory, the preset will return This.webHDFSClient.DeleteDirectory (path) under the root directory "/". Result; } public string CreateFile (string localfile, String remotepath) {//The incoming remote path does not contain a root directory, the preset will return under root "/" thi S.webhdfsclient.createfile (LocalFile, RemotePath). Result; When the public bool DeleteFile (string path) {//incoming path does not contain a root directory, the preset will return under the root directory "/" This.webHDFSClient.DeleteDirect Ory (Path). Result; When the public httpresponsemessage OpenFile (string path) {//incoming path does not contain a root directory, the preset will return under the root directory "/" this.webhdfsclient . OpenFile (Path). Result; } }}
Program.cs
Using physical;using system;using system.collections.generic;using system.io;using system.linq;using System.Text; Using System.threading.tasks;namespace mytest{class Program {//HDFS cluster client entry endpoint set on host//preset endpoint: http://[host name ]:50070//Preset account: Wu private static hdfsaccess access = new Hdfsaccess (@ "http://127.0.0.1:50070", "WU"); static void Main (string[] args) {getdirectoriestest (); Console.WriteLine ("----------------------------------------------"); Getfilestest (); Console.WriteLine ("----------------------------------------------"); Directorytest (); Console.WriteLine ("----------------------------------------------"); Filetest (); Console.WriteLine ("----------------------------------------------"); Openfiletest (); Console.readkey (); } public void TestCleanup () {//Gets the root folder var directories = Access. GetDirectories (@ "/"); Remove the preset directory: TMP, the directory outside of user foreach (var directory in directories) { if ("TMP"). Equals (directory) | | "User". Equals (directory)) {continue; } else {access. DeleteDirectory (directory); }}//Get root directory file var files = Access. GetFiles (@ "/"); Remove all files, foreach (var file in files) {access. DeleteFile (file); }//Remove OpenFile dump file File.delete (Path.Combine (Directory.GetCurrentDirectory (), "test.jpg")); //_ Incoming root directory _ expected callback preset directory public static void Getdirectoriestest () {//preset root directory has two directories: TMP, user var expected = NE W list<string> () {"TMP", "User",}; var actual = access. GetDirectories (@ "/"); foreach (var item in actual) {Console.WriteLine (item); }}//_ Incoming root directory _ expected callback empty collection public static void Getfilestest () {//preset root no file var expected = new List<st Ring> (); var actual = access. GetFiles (@ "/"); foreach (var item in actual) {Console.WriteLine (item); }}//_ building the ZZZ directory_ Expected Success _ expected root directory has the zzz directory _ Delete zzz Directory _ expected Success _ expected root directory without the zzz directory public static void Directorytest () {var directoryname = "zzz"; Build the zzz directory var boolcreatedirectory = access. CreateDirectory (directoryname); Console.WriteLine ("Build zzz Directory _ Expected success:" +boolcreatedirectory); Establish the ZZZ directory _ expected Success _ expected root directory has zzz directory//Preset root directory has three directories: TMP, user, zzz var expectedcreatedirectory = new List<string> () {"TMP", "User", DirectoryName,}; var actualcreatedirectory = access. GetDirectories (@ "/"); foreach (var item in actualcreatedirectory) {Console.WriteLine (item); } Console.WriteLine ("********************************************"); Delete the zzz directory var booldeletedirectory = access. DeleteDirectory (directoryname); Console.WriteLine ("Delete zzz Directory _ expected success:" + booldeletedirectory); Delete the zzz directory _ expected Success _ expected root directory no zzz directory//default root directory has two directories: TMP, user var expecteddeletedirectory = new list<string> () {"T MP "," User ",}; var actualdeletedirectory = access. GetdirectoRies (@ "/"); foreach (var item in actualdeletedirectory) {Console.WriteLine (item); }}//_ Build test file _ expected root directory has test file _ Delete test File _ Expected Success _ expected root directory without test file public static void Filetest () {var localfile = Path.Combine (Directory.GetCurrentDirectory (), "TestFolder", "test.jpg"); var RemotePath = "Test.jpg"; Create Test file var boolcreatefile = access. CreateFile (LocalFile, RemotePath); Create test file _ expected root directory has test file var expectedcreatefile = new List<string> () {remotepath,}; var actualcreatefile = access. GetFiles (@ "/"); foreach (var item in Actualcreatefile) {Console.WriteLine (item); } Console.WriteLine ("********************************************"); Delete the test file var booldeletefile = access. DeleteDirectory (RemotePath); Console.WriteLine ("Delete test file _ expected success:" +booldeletefile); Delete test File _ Expected Success _ expected root directory without test file var expecteddeletefile = new list<string> (); var actualdeletefile = access. GEtfiles (@ "/"); foreach (var item in Actualdeletefile) {Console.WriteLine (item); }}//_ Create Test file _ expected root directory has test file _ Get test File _ Expected Success _ expected callback test file stream and dump success public static void Openfiletest () {var LocalFile = Path.Combine (Directory.GetCurrentDirectory (), "TestFolder", "test.jpg"); var RemotePath = "Test.jpg"; var saveFile = Path.Combine (Directory.GetCurrentDirectory (), "test.jpg"); Console.WriteLine ("SaveFile:" + saveFile); Console.WriteLine ("********************************************"); Create Test file var boolcreatefile = access. CreateFile (LocalFile, RemotePath); Create test file _ expected root directory has test file var expectedcreatefile = new List<string> () {remotepath,}; var actualcreatefile = access. GetFiles (@ "/"); foreach (var item in Actualcreatefile) {Console.WriteLine (item); } Console.WriteLine ("********************************************"); Get the test file var response = Access. OpenFile (reMotepath); Obtained test file _ expected success response. Ensuresuccessstatuscode (); Get the test file _ expected Success _ expected callback test file stream and dump the successful using (var fs = File.create (SaveFile)) {response. Content.copytoasync (FS). Wait (); } Console.WriteLine (File.exists (SaveFile)); } }}
Operation Result: