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
{
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(@"/");
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);
}
File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Test.jpg"));
}
public static void GetDirectoriesTest()
{
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);
}
}
public static void DirectoryTest()
{
var directoryName = "zzz";
var boolCreateDirectory = access.CreateDirectory(directoryName);
Console.WriteLine("建立zzz目录_预期成功:"+boolCreateDirectory);
var expectedCreateDirectory = new List<string>() { "tmp", "user", directoryName, };
var actualCreateDirectory = access.GetDirectories(@"/");
foreach (var item in actualCreateDirectory)
{
Console.WriteLine(item);
}
Console.WriteLine("********************************************");
var boolDeleteDirectory = access.DeleteDirectory(directoryName);
Console.WriteLine("删除zzz目录_预期成功:" + boolDeleteDirectory);
var expectedDeleteDirectory = new List<string>() { "tmp", "user", };
var actualDeleteDirectory = access.GetDirectories(@"/");
foreach (var item in actualDeleteDirectory)
{
Console.WriteLine(item);
}
}
public static void FileTest()
{
var localFile = Path.Combine(Directory.GetCurrentDirectory(), "TestFolder", "Test.jpg");
var remotePath = "Test.jpg";
var boolCreateFile = access.CreateFile(localFile, remotePath);
var expectedCreateFile = new List<string>() { remotePath, };
var actualCreateFile = access.GetFiles(@"/");
foreach (var item in actualCreateFile)
{
Console.WriteLine(item);
}
Console.WriteLine("********************************************");
var boolDeleteFile = access.DeleteDirectory(remotePath);
Console.WriteLine("删除Test档案_预期成功:"+boolDeleteFile);
var expectedDeleteFile = new List<string>();
var actualDeleteFile = access.GetFiles(@"/");
foreach (var item in actualDeleteFile)
{
Console.WriteLine(item);
}
}
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("********************************************");
var boolCreateFile = access.CreateFile(localFile, remotePath);
var expectedCreateFile = new List<string>() { remotePath, };
var actualCreateFile = access.GetFiles(@"/");
foreach (var item in actualCreateFile)
{
Console.WriteLine(item);
}
Console.WriteLine("********************************************");
var response = access.OpenFile(remotePath);
response.EnsureSuccessStatusCode();
using (var fs = File.Create(saveFile))
{
response.Content.CopyToAsync(fs).Wait();
}
Console.WriteLine(File.Exists(saveFile));
}
}
}
运行结果如图:
