ASP.NET实现Hadoop增删改查的示例代码
程序员文章站
2022-07-06 12:50:33
本文介绍了asp.net实现hadoop增删改查的示例代码,分享给大家,具体如下:
packages.config
本文介绍了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 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.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)); } } }
运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 要提升便利店坪效?你必须掌握这五大技能