本文介绍了ASP.NET实现Hadoop增删改查的示例代码,分享给大家,具体如下:
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 @R_419_4609@ 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.GetDirectoryStatus(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(); } public bool CreateDirectory(string path) { // 传入路径不包含根目录时,预设会在根目录「/」底下 return this.webHDFSClIEnt.CreateDirectory(path).Result; } public bool DeleteDirectory(string path) { // 传入路径不包含根目录时,预设会在根目录「/」底下 return this.webHDFSClIEnt.DeleteDirectory(path).Result; } public string Createfile(string localfile, string remotePath) { // 传入远端路径不包含根目录时,预设会在根目录「/」底下 return this.webHDFSClIEnt.Createfile(localfile, remotePath).Result; } public bool Deletefile(string path) { // 传入路径不包含根目录时,预设会在根目录「/」底下 return this.webHDFSClIEnt.DeleteDirectory(path).Result; } public httpResponseMessage Openfile(string path) { // 传入路径不包含根目录时,预设会在根目录「/」底下 return 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 客户端进入端点设定于 主机上 // 预设端点:http://[主机名称]:50070 // 预设帐号: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() { //取得根目录资料夹 var directorIEs = access.GetDirectorIEs(@"/"); // 移除预设目录:tmp、user 外的目录 foreach (var directory in directorIEs) { if ("tmp".Equals(directory) || "user".Equals(directory)) { continue; } else { access.DeleteDirectory(directory); } } // 取得根目录档案 var files = access.Getfiles(@"/"); // 移除所有档案 foreach (var file in files) { access.Deletefile(file); } // 移除 Openfile 转存档案 file.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Test.jpg")); } //_传入根目录_预期回传预设目录 public static voID GetDirectorIEstest() { // 预设根目录下有两个目录:tmp、user var expected = new List<string>() { "tmp", "user", }; var actual = access.GetDirectorIEs(@"/"); foreach (var item in actual) { Console.Writeline(item); } } //_传入根目录_预期回传空集合 public static voID Getfilestest() { // 预设根目录下没有档案 var expected = new List<string>(); var actual = access.Getfiles(@"/"); foreach (var item in actual) { Console.Writeline(item); } } //_建立zzz目录_预期成功_预期根目录下有zzz目录_删除zzz目录_预期成功_预期根目录下无zzz目录 public static voID Directorytest() { var directoryname = "zzz"; // 建立zzz目录 var boolCreateDirectory = access.CreateDirectory(directoryname); Console.Writeline("建立zzz目录_预期成功:"+boolCreateDirectory); // 建立zzz目录_预期成功_预期根目录下有zzz目录 // 预设根目录下有三个目录: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("********************************************"); // 删除zzz目录 var boolDeleteDirectory = access.DeleteDirectory(directoryname); Console.Writeline("删除zzz目录_预期成功:" + boolDeleteDirectory); // 删除zzz目录_预期成功_预期根目录下无zzz目录 // 预设根目录下有两个目录:tmp、user var expectedDeleteDirectory = new List<string>() { "tmp", "user", }; var actualDeleteDirectory = access.GetDirectorIEs(@"/"); foreach (var item in actualDeleteDirectory) { Console.Writeline(item); } } //_建立Test档案_预期根目录下有Test档案_删除Test档案_预期成功_预期根目录下无Test档案 public static voID filetest() { var localfile = Path.Combine(Directory.GetCurrentDirectory(), "TestFolder", "Test.jpg"); var remotePath = "Test.jpg"; // 建立Test档案 var boolCreatefile = access.Createfile(localfile, remotePath); // 建立Test档案_预期根目录下有Test档案 var expectedCreatefile = new List<string>() { remotePath, }; var actualCreatefile = access.Getfiles(@"/"); foreach (var item in actualCreatefile) { Console.Writeline(item); } Console.Writeline("********************************************"); // 删除Test档案 var boolDeletefile = access.DeleteDirectory(remotePath); Console.Writeline("删除Test档案_预期成功:"+boolDeletefile); // 删除Test档案_预期成功_预期根目录下无Test档案 var expectedDeletefile = new List<string>(); var actualDeletefile = access.Getfiles(@"/"); foreach (var item in actualDeletefile) { Console.Writeline(item); } } //_建立Test档案_预期根目录下有Test档案_取得Test档案_预期成功_预期回传Test档案Stream并转存成功 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("********************************************"); // 建立Test档案 var boolCreatefile = access.Createfile(localfile, remotePath); // 建立Test档案_预期根目录下有Test档案 var expectedCreatefile = new List<string>() { remotePath, }; var actualCreatefile = access.Getfiles(@"/"); foreach (var item in actualCreatefile) { Console.Writeline(item); } Console.Writeline("********************************************"); // 取得Test档案 var response = access.Openfile(remotePath); // 取得Test档案_预期成功 response.EnsureSuccessstatusCode(); // 取得Test档案_预期成功_预期回传Test档案Stream并转存成功 using (var fs = file.Create(savefile)) { response.Content.copyToAsync(fs).Wait(); } Console.Writeline(file.Exists(savefile)); } }}
运行结果如图:
总结以上是内存溢出为你收集整理的ASP.NET如何实现Hadoop增删改查全部内容,希望文章能够帮你解决ASP.NET如何实现Hadoop增删改查所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)