packages.config
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.Hadoop.WebClient" version="0.12.5126.42915" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
<package id="System.Spatial" version="5.2.0" targetFramework="net45" />
<package id="WindowsAzure.Storage" version="2.0.4.1" targetFramework="net45" />
packages>
Explorer.cs
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Hadoop.WebHDFS;
namespace HdfsExplore
{
public partial class Explorer : Form
{
private WebHDFSClient client;
public Explorer()
{
InitializeComponent();
if (!CreateClient())
{
return;
}
PopulateDriveList();
}
private async void PopulateDriveList()
{
TreeNode nodeTreeNode;
var imageIndex = 0;
var selectIndex = 0;
const int Removable = 2;
const int LocalDisk = 3;
const int Network = 4;
const int CD = 5;
Cursor = Cursors.WaitCursor;
tvFolders.Nodes.Clear();
nodeTreeNode = new TreeNode("Hadoop Root", 0, 0);
tvFolders.Nodes.Add(nodeTreeNode);
var nodeCollection = nodeTreeNode.Nodes;
var x = await client.GetDirectoryStatus("/");
foreach (var mo in x.Directories)
{
imageIndex = 6;
selectIndex = 6;
nodeTreeNode = new TreeNode(mo.PathSuffix, imageIndex, selectIndex);
nodeCollection.Add(nodeTreeNode);
}
InitListView();
Cursor = Cursors.Default;
}
private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
{
Cursor = Cursors.WaitCursor;
var nodeCurrent = e.Node;
nodeCurrent.Nodes.Clear();
if (nodeCurrent.SelectedImageIndex == 0)
{
PopulateDriveList();
}
else
{
PopulateDirectory(nodeCurrent, nodeCurrent.Nodes);
}
Cursor = Cursors.Default;
}
protected void InitListView()
{
lvFiles.Clear();
lvFiles.Columns.Add("名称", 150, HorizontalAlignment.Left);
lvFiles.Columns.Add("大小", 75, HorizontalAlignment.Right);
lvFiles.Columns.Add("上次访问", 140, HorizontalAlignment.Left);
lvFiles.Columns.Add("上次修改", 140, HorizontalAlignment.Left);
lvFiles.Columns.Add("用有者", 75);
lvFiles.Columns.Add("权限", 75);
}
protected async void PopulateDirectory(TreeNode nodeCurrent, TreeNodeCollection nodeCurrentCollection)
{
TreeNode nodeDir;
var imageIndex = 2;
var selectIndex = 3;
if (nodeCurrent.SelectedImageIndex != 0)
{
try
{
var directoryStatus = await client.GetDirectoryStatus(getFullPath(nodeCurrent.FullPath));
var dirs = directoryStatus.Directories;
PopulateFiles(nodeCurrent);
var stringDirectories = dirs.Select(x => x.PathSuffix).ToArray();
var stringFullPath = "";
var stringPathName = "";
foreach (var stringDir in stringDirectories)
{
stringFullPath = stringDir;
stringPathName = GetPathName(stringFullPath);
nodeDir = new TreeNode(stringPathName, imageIndex, selectIndex);
nodeCurrentCollection.Add(nodeDir);
}
}
catch (IOException e)
{
MessageBox.Show("Error: Drive not ready or directory does not exist.");
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("Error: Drive or directory access denided.");
}
catch (Exception e)
{
MessageBox.Show("Error: " + e);
}
}
}
protected string GetPathName(string stringPath)
{
var stringSplit = stringPath.Split('\\');
var _maxIndex = stringSplit.Length;
return stringSplit[_maxIndex - 1];
}
protected async void PopulateFiles(TreeNode nodeCurrent)
{
var lvData = new string[6];
InitListView();
if (nodeCurrent.SelectedImageIndex != 0)
{
var directoryStatus = await client.GetDirectoryStatus(getFullPath(nodeCurrent.FullPath));
try
{
var files = directoryStatus.Files;
var stringFileName = "";
string dtCreateDate, dtModifyDate;
Int64 lFileSize = 0;
foreach (var file in files)
{
stringFileName = file.PathSuffix;
lFileSize = file.Length;
dtCreateDate = Utility.JavaTicksToDatetime(file.AccessTime).ToString();
dtModifyDate = Utility.JavaTicksToDatetime(file.ModificationTime).ToString();
lvData[0] = GetPathName(stringFileName);
lvData[1] = formatSize(lFileSize);
lvData[2] = dtCreateDate;
lvData[3] = dtModifyDate;
lvData[4] = file.Owner;
lvData[5] = file.Permission;
var lvItem = new ListViewItem(lvData, 0);
lvFiles.Items.Add(lvItem);
}
}
catch (IOException e)
{
MessageBox.Show("错误:驱动器未准备好或目录不存在.");
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("错误:驱动器或目录访问被拒绝.");
}
catch (Exception e)
{
}
}
}
protected string getFullPath(string stringPath)
{
var stringParse = "";
stringParse = stringPath.Replace("Hadoop Root\\", "/").Replace("\\", "/");
return stringParse;
}
protected string getFullPath(TreeNode nodeCurrent)
{
var path = nodeCurrent.FullPath;
return getFullPath(path);
}
protected string formatSize(Int64 lSize)
{
var stringSize = "";
var myNfi = new NumberFormatInfo();
Int64 lKBSize = 0;
if (lSize < 1024)
{
if (lSize == 0)
{
stringSize = "0";
}
else
{
stringSize = "1";
}
}
else
{
lKBSize = lSize / 1024;
stringSize = lKBSize.ToString("n", myNfi);
stringSize = stringSize.Replace(".00", "");
}
return stringSize + " KB";
}
private void menuItem2_Click(object sender, EventArgs e)
{
Close();
}
private void tvFolders_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
if (e.Node == null) return;
tvFolders.SelectedNode = e.Node;
InitcmsOneRout();
cmsOneRout.Show(tvFolders, e.X, e.Y);
}
private void InitcmsOneRout()
{
cmsOneRout = new ContextMenuStrip();
var tmiEditRoutStation = new ToolStripMenuItem("修改Owner");
cmsOneRout.Items.Add(tmiEditRoutStation);
var changePermission = new ToolStripMenuItem("修改权限");
changePermission.Click += changePress_Click;
cmsOneRout.Items.Add(changePermission);
var tmiDel = new ToolStripMenuItem("删除");
tmiDel.Click += tmiDel_Click;
cmsOneRout.Items.Add(tmiDel);
}
async void changePress_Click(object sender, EventArgs e)
{
if (tvFolders.SelectedNode == null)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
var permissions = await client.GetFileStatus(remotepath);
InputName ip = new InputName("修改权限(" + remotepath + ")", permissions.Permission);
if (ip.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrWhiteSpace(ip.StrInput))
{
MessageBox.Show("不能为空项");
return;
}
await client.SetPermissions(remotepath, ip.StrInput);
}
}
private void tmiDel_Click(object sender, EventArgs e)
{
var path = getFullPath(tvFolders.SelectedNode);
client.DeleteDirectory(path, true);
MessageBox.Show("删除完成");
tvFolders.SelectedNode.Remove();
}
private void menuItem5_Click(object sender, EventArgs e)
{
var parentPath = "/";
if (tvFolders.SelectedNode != null)
{
parentPath = getFullPath(tvFolders.SelectedNode);
}
var name = new InputName();
if (name.ShowDialog() != DialogResult.OK)
return;
var newname = name.StrInput;
client.CreateDirectory(parentPath.TrimEnd('/') + "/" + newname);
}
private void menuItem6_Click(object sender, EventArgs e)
{
showSetting();
PopulateDriveList();
}
private void showSetting()
{
var setting = new Setting();
if (setting.ShowDialog() == DialogResult.OK)
{
CreateClient();
}
}
private bool CreateClient()
{
var url = AppConfig.GetValue("hdfsurl");
var username = AppConfig.GetValue("username");
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(username))
{
MessageBox.Show("未进行正确配置");
showSetting();
return false;
}
client = new WebHDFSClient(new Uri(url), username);
return true;
}
private void menuItem7_Click(object sender, EventArgs e)
{
if (tvFolders.SelectedNode == null)
{
MessageBox.Show("未选中上传目标");
return;
}
OpenFileDialog of = new OpenFileDialog();
of.Multiselect = true;
if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var remotepath = getFullPath(tvFolders.SelectedNode);
var filename = of.FileNames;
foreach (var s in filename)
{
client.CreateFile(s, remotepath.TrimEnd('/') + "/" + Path.GetFileName(s));
}
PopulateFiles(tvFolders.SelectedNode);
}
}
private void menuItem8_Click(object sender, EventArgs e)
{
var items = lvFiles.SelectedItems;
var remotepath = getFullPath(tvFolders.SelectedNode);
foreach (var item in items)
{
var filename = ((ListViewItem)item).Text;
client.DeleteDirectory(remotepath.TrimEnd('/') + "/" + filename);
}
}
private async void lvFiles_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = lvFiles.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
var remotepath = getFullPath(tvFolders.SelectedNode);
var remotefile = remotepath.TrimEnd('/') + "/" + item.Text;
var message = await client.OpenFile(remotefile);
var content = await message.Content.ReadAsStringAsync();
ContentViewer cv = new ContentViewer(content, remotefile);
cv.Show();
}
else
{
this.lvFiles.SelectedItems.Clear();
MessageBox.Show("No Item is selected");
}
}
private void lvFiles_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (this.lvFiles.SelectedItems.Count > 0)
{
var cmslv = new ContextMenuStrip();
var tmiEditRoutStation = new ToolStripMenuItem("权限");
tmiEditRoutStation.Click += tmiEditRoutStation_Click;
cmslv.Items.Add(tmiEditRoutStation);
var tmiDelte = new ToolStripMenuItem("删除");
tmiDelte.Click += tmiDelte_Click;
cmslv.Items.Add(tmiDelte);
var downloadFile = new ToolStripMenuItem("下载");
downloadFile.Click += downloadFile_Click;
cmslv.Items.Add(downloadFile);
cmslv.Show(lvFiles, e.Location);
}
else
{
}
}
}
async void downloadFile_Click(object sender, EventArgs e)
{
if (lvFiles.SelectedItems.Count == 0)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
var filefolder = new FolderBrowserDialog();
if (filefolder.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
var locafolder = filefolder.SelectedPath;
foreach (var lvFile in lvFiles.SelectedItems)
{
var removefilename = ((ListViewItem)lvFile).Text;
var remotefile = remotepath.TrimEnd('/') + "/" + removefilename;
var file = await client.OpenFile(remotefile);
var filebytes = await file.Content.ReadAsByteArrayAsync();
var locafile = locafolder + "/" + removefilename;
System.IO.File.WriteAllBytes(locafile, filebytes);
}
MessageBox.Show("下载成功");
}
async void tmiDelte_Click(object sender, EventArgs e)
{
if (lvFiles.SelectedItems.Count == 0)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
foreach (var lvFile in lvFiles.SelectedItems)
{
var remotefile = remotepath.TrimEnd('/') + "/" + ((ListViewItem)lvFile).Text;
await client.DeleteDirectory(remotefile);
lvFiles.Items.Remove((ListViewItem)lvFile);
}
}
async void tmiEditRoutStation_Click(object sender, EventArgs e)
{
if (lvFiles.SelectedItems.Count == 0)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
var remotefile = remotepath.TrimEnd('/') + "/" + lvFiles.SelectedItems[0].Text;
InputName ip = new InputName("修改权限(" + remotefile + ")");
if (ip.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrWhiteSpace(ip.StrInput))
{
MessageBox.Show("不能为空项");
return;
}
await client.SetPermissions(remotefile, ip.StrInput);
}
}
}
}
运行结果如图: